Text of JLabel moves out of JPanel - swing

I have a JPanel with specific size. I have added a JLabel on it. Now the text of JLabel is dynamic and comes from the database. When I add this data to JLabel, instead of it staying in the JPanel, it spreads out.
When it happens, the data inside the JPanel is visible but the one outside the JPanel is not visible (which is obvious). I want to know if there is a way by which the data instead of going out of JPanel, will stay in JPanel itself (means it automatically adds a newline and moves to newline instead of going out).

JLabel doesn't have a method for that. But you have a few options:
Use HTML tags in text you set "Hello<br/>World".
Use JTextArea or JEditorPane and disable editing. JTextArea have the setLineWrap(true) method, and JEditorPane wraps lines by default.

You'll want to use something more robust than a simple JLabel, like a JTextArea. Your best place to learn how to achieve the desired effect would be to read the Swing tutorial here. Since you know the size of your JPanel you can set the size of your JTextArea and it should automatically line-wrap to accommodate for your variable-sized strings.

Related

Is JPanel supposed to be added to a JFrame?

I have this code which I don't understand:
JFrame jframe = new JFrame("Risultati protocolli cercati");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel body = new JPanel();
Container c = jframe.getContentPane();
body.setSize(100, 100);
body.setLayout(new GridLayout(1000, 1));
for (int i = 0; i < 1000; i++)
body.add(new JLabel("JLabel " + i));
JScrollPane jsp = new JScrollPane(body);
c.add(jsp);
jframe.setSize(100, 100);
//jframe.add(body);
jframe.setVisible(true);
If I leave the penultimate line commented then everything appears, both labels and scroll. Instead if I uncomment that line, I see nothing. Only the JFrame. Why does it happens? For the main window of my program I had to perform jframe.add(body)...
Instead if I uncomment that line, I see nothing. Why does it happens?
The below line
jframe.add(body);
internally calls
jframe.getContentPane().add(body);
JFrame by default uses BorderLayout and add(component) method directly add it in the center section, if you add again then last one is replaced with latest one.
You can use overloaded add() method of JFrame to add it in another section east, west, north and south as shown in below snapshot:
for e.g.:
frame.add(comp,BorderLayout.NORTH); // add in north section
frame.add(comp,BorderLayout.WEST); // add in west section
You can use other Layout Managers as well as per the design or your application:
It's worth reading How to Use BorderLayout
One more suggestion:
Use frame.pack() instead of frame.setSize() that fits the components as per component's preferred size.
Interesting problem caused by the layout managers.
A component can only have a single parent. First you add the "body" to the scrollpane but then the "body" gets removed from the scrollpane when you add it to the content pane of the frame (for the reasons mentioned by #braj). Not a big deal as it just means you won't see any scrollbars.
Since the component is directly added to the content pane you should still see the labels however they do not display and this is the confusing part. Change your code to use "100" for the GridLayout and the number of components you create in the loop. When the frame first displays the panel will be empty. Now, increase the height of the frame and you will see the components appear. What is happening is that you are trying to paint too many components in a small area and because of rounding issues the height of every component becomes 0, so there is nothing to paint. When you increase the height to at least 100 pixels every component can now be 1 pixel high so you get garbage.
The only solution is to keep the "body" panel in the scrollpane so that all components will be displayed at their preferred size. Then you can scroll through all the components as required.
A tip for when using a GridLayou. You can use:
body.setLayout(new GridLayout(0, 1));
Then means the grid will have unlimited rows and a single column.

JPanel Containts inside JFrame

I am Having a JTextField in JFrame having mouselistener on it,and i want to display JPanel with some contents when mouse event is fired.JPanel is displaying but the contents in it are not visible.
you cannot write text directly inside JPanel. Use a JLabel and put the text inside that JLabel and then place the JLabel on the JPanel
You have to call revalidate() after adding new panel, which will instruct the layout manager to recalculate the layout and panel.repaint(); if necessary.
If this didn't solve your problem, post an SSCCE

JScrollPane will not reduce in size

I wrote a simple gui with DesignGridLayout as layout manager.
I have a 2 JTextFields, beneath them a JScrollPane containing a JTextArea, and right under it I have a button.
Upon maximizing the JFrame, everything is stretching out nicely to fill the screen.
The problem starts when I try to restore the Frame to it's original size. the JScrollFrame Vertical size remains too large, and the Button is not shown, since it is covered by the too big Jscrollpane.
The problem is the same for manual resizing. The JScrollPane sets a new value for it's vertical size according to how much I stretch it.
I tried overriding the getScrollableTracksViewportHeight() method from Scrollable interface, but it had no affect. In addition, I had this entire setting inside a Jpanel which I removed, but it's still the same.
Any insights would be appreciated.
edit: added samples of code.
This code creates most of the Gui:
DesignGridLayout layout = new DesignGridLayout(frame);
layout.row().grid().empty().add(new JLabel("someText1"), someText1);
layout.row().grid().empty().add(new JLabel("someText2"), someText2);
layout.emptyRow();
layout.row().grid().add(new JSeparator());
layout.row().grid().empty().add(titleLabel).empty();
layout.row().grid().add(textarea("",5,6));
layout.row().grid().empty().add(button).empty();
This code creates The JScrollPane:
private JScrollPane textarea(String content, int rows, int columns)
{
JTextArea textArea = new JTextArea(rows, columns);
JScrollPane scrollPane = new JScrollPane(textArea);
return scrollPane;
}

Scrollable html JLabel

I have a Jlabel packed with html. I would like to scroll this content as you would with overflow : auto; in css. I can't seem to get this to work. Has anyone come across this? I'd like to keep the content in HTML - for mark up - and use something light to scroll though it.
BTW: The Jlabel is in a popup - I can use something else other than a JLabel if needs be but would like to keep the html.
Cheers,
slotishtype
Put that JLabel in a JScrollPane instanciated that way :
JScrollPane scroller = new JScrollPane(myJLabel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Notice that, as this constructor states, those parameters can be changed afterwards.

swing: appropriate layout manager for simple situation?

I have a JPanel that I want to use to contain 3 vertical components:
a JLabel
a JTextField
a JScrollPane
I want all 3 components to fill the JPanel's width. I want the JLabel and JTextField to use their normal heights and the JScrollPane to use the rest.
BoxLayout almost works, except it seems like the JTextField and JScrollPane share the "extra" space when the JPanel is made large.
What can I do?
Create a BorderLayout. Put the JScrollPane in its center.
Create a JPanel with a BoxLayout. Put the JLabel and JTextField in that, vertically. Put that JPanel into the NORTH side of the BorderLayout.
GridBagLayout is pretty handy. You can control anything you need and you can control only what you need. You're probably going to be interested in only the vertical parameters.
You could also use DesignGridLayout as follows:
DesignGridLayout layout = new DesignGridLayout(thePanel);
layout.row().center().fill().add(theLabel);
layout.row().center().fill().add(theTextField);
layout.row().center().fill().add(theScrollPane);
This should exactly behave as you describe.
Each call to row() creates a new row in the panel.
The calls to fill() make sure that each component uses the whole available width.
A few advantages of using DesignGridLayout here are:
only one LayoutManager for the whole
panel
automatic borders and inter-rows spacing