1

This is what I've layed out using a GridBagLayout:

normal layout

But, when I select a file a couple of labels are supposed to be populated. This is what it looks like after I make a file selection using those small "..." buttons:

messed layout

As you can see , the entire layout gets messed up. All I am doing in the actionlistener is this:

            fileTxt = fileChooser.getSelectedFile();
            fileTxtField.setText(fileTxt.getAbsolutePath());
            labels = getLabels(); 
            lbl1.setText(labels[0].toUpperCase());
            Dimension size = lbl1.getPreferredSize();
            lbl1.setMinimumSize(size);
            lbl1.setPreferredSize(size);
            lbl2.setText(labels[1]);
            lbl2.setToolTipText(longLbl);
            size = lbl2.getPreferredSize();
            lbl2.setMinimumSize(size);
            lbl2.setPreferredSize(size);
            button1.setPreferredSize(new Dimension(20,25));
            button2.setPreferredSize(new Dimension(20,25));

So, basically, the buttons are going to their original sizes and not preferred sizes.and that messes up the entire layout. How do I fix this? All components are set not to fill with the gridbagconstraint of gridBagConstraints.fill set to GridBagConstraints.NONE - however, the layout still gets messed up :(

UPDATE

As per your suggestions, I removed the code that was calling setPreferredSize() method, and this is what I get:

enter image description here

Obviously, this is what I want to avoid - a button, that is bigger than its text, that was reason for setting setPreferredSize on the button. Now what do I do?

Jay
  • 2,394
  • 11
  • 54
  • 98
  • Why is there a need to reset the size. You can set it invisible in the first place while creating the layout and make them visible in the action listener. Also the grid bag doesn't seem to be properly set as the components besides the labels are popping out of the normal grid. You should set the grid bag constants for the components. It gives a more aligned layout(the purpose of grid bag layout) – Nitin Chhajer Apr 26 '12 at 08:14
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Best to suggest a size for the text fields in the constructor. 3) Be sure to call `pack()` on the container. 4) Layouts sometimes respect the preferred size of components, other times not. 5) This looks well suited to [`GroupLayout`](http://docs.oracle.com/javase/7/docs/api/javax/swing/GroupLayout.html). – Andrew Thompson Apr 26 '12 at 08:35
  • @NitinChhajer added an update – Jay Apr 26 '12 at 19:20

2 Answers2

5

Don't call setPreferredSize(). Let LayoutManager do this.

In your GridBagConstraints define fill and weightx parameters to define how extra space should be distributed.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Even though this answer helped me in getting rid of setPrefferredSize method, Nitin's answer rightly solved my UI problem. Hence, I am upvoting and this and awarding Nitin's answer. – Jay Apr 26 '12 at 21:51
3

There is a simple solution to this. The extra width of the buttons is due to the default margins on the left and right of the button text. By default it is 14

you can set btn.setMargin(new Insets(2, 0, 2, 0)); and that extra gap will go.

Nitin Chhajer
  • 2,299
  • 1
  • 24
  • 35