sumu00
sumu00

Reputation: 49

Setting size on JLabel displaces other components in java

I am setting a JLabel for the error messages in my program, so initially the label is empty label.setText(""), but when there is an error it should change to something like label.setText("Error, you have entered invalid data...").

If I use setSize(x,y) on the label, it forces other components to displace when error message takes place. But using setPreferredSize(Dimension(x,y))doesn't impact them.

Q1. Why is that?

Q2. What is the difference between setSize(x,y) and setPreferredSize(Dimension(x,y))

Q3. Does it have to do anything with layout?

Thank you in advance for explanation!

P.S. I am using GridBagLayout for positioning my components on the JPanel.

Upvotes: 0

Views: 116

Answers (2)

VGR
VGR

Reputation: 44338

Don’t use the setSize method.

setSize is called by LayoutManagers, like GridBagLayout, to lay out child components. When you call setSize explicitly, you are fighting with the GridBagLayout. Eventually, GridBagLayout will undo your setSize call, when it calls setSize for its own purposes.

In other words, any call to setSize eventually will be wiped out by the parent layout.

setPreferredSize will not be wiped out. Most LayoutManagers, including GridBagLayout, do their best to respect a component’s preferred size.

However, you should not be calling setPreferredSize. Components already have a preferred size by default, and it is almost certainly better than any numbers you can come up with. For instance, a JLabel’s default preferred size is the size which is just large enough to accommodate its text, icon, and borders.

Computing a preferred size is harder than you might think. How many pixels does text use? How many pixels high is a 12 point font? 12 points is not 12 pixels. 12 points is 1272 inch. How many pixels is that? It depends on the user’s monitor and graphics resolution. All of this is known to the Swing rendering system, and JLabel uses all of that information to determine its default preferred size. You should not try to reinvent all of that work, and you should not try to replace that work with something simpler, as it will be inadequate.

If you just let the JLabel keep its preferred size, GridBagLayout will do its best to accommodate that. If the window itself does not have room to display the JLabel’s new text, you probably should call the window’s pack() method after changing the text.

Update: This appears to be an XY problem—you really want a message that you can show and hide.

You want your layout to be big enough to accommodate your message text as soon as you create it. This is typically done with a CardLayout, which lets you place several components on top of each other, with only one of them visible at any given moment. Since you want to show no text at all, initially, you would add an empty JLabel as the first component in the CardLayout, so it is shown by default:

JLabel label = new JLabel("Error, you have entered invalid data...");

CardLayout messageLayout = new CardLayout();
JPanel messagePane = new JPanel(messageLayout);
messagePane.add(new JLabel(), "blank");
messagePane.add(label, "message");

// Do not add label directly to your user interface.
// Add messagePane instead.
mainWindow.add(messagePane);

// ...

// Show message
messageLayout.show(messagePane, "message");

// ...

// Hide message
messageLayout.show(messagePane, "blank");

"message" and "blank" are never seen by the user. They are just unique identifiers for each component (“card”) in the CardLayout. You can make them anything you want.

Upvotes: 5

DarkCruncher
DarkCruncher

Reputation: 41

The setSize() function sets the size not based on any LayoutManager. Thats why you should always use setPrefferedSize() when working with a LayoutManager. setPrefferedSize() firstly tries to be conform with the LayoutManagers dimensions if then possible Java tries to set the size of the Label according to your setPrefferedSize() input.

So yes, it does have anything to do with layout. If possible, you should only use setPrefferedSize() as you are working with layout managers.

Upvotes: 2

Related Questions