2

I have a window that uses a GridBagLayout as its layout. On the left side are some JComboBoxes with some labels describing what they are, and on the right, a few JPanels and a JLabel giving a description of the selected item in the respective JComboBox. Both columns have equal weights horizontally. However, the description that is a JLabel (in bold above), although its text is in <html> tags to make it wrap, it sets its preferred size to fit its entire length on one line, which makes it too wide, and the columns don't resize properly.

The layout is like this:

┌────────────────────┬────────────────────┐
│       JLabel       │                    │
├────────────────────┤                    │
│                    │ Description JPanel │
│     JComboBox      │                    │
│                    │                    │
├────────────────────┼────────────────────┤
│       JLabel       │                    │
├────────────────────┤                    │
│                    │ Description JPanel │
│     JComboBox      │                    │
│                    │                    │
├────────────────────┼────────────────────┤
│       JLabel       │                    │
├────────────────────┤    *Bothersome*    │
│                    │                    │
│     JComboBox      │      *JLabel*      │
│                    │                    │
├────────────────────┴────────────────────┤
│             JButton (Done)              │
└─────────────────────────────────────────┘

What I want to know is, how do I make the GridBagLayout ignore the preferred size, or how can I make the two columns equal, regardless of the components in them?

Thanks in advance.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ricky3350
  • 1,710
  • 3
  • 20
  • 35

3 Answers3

0

You can make something like this:

JPanel mainPanel = new JPanel(new GridLayout(1,2));
mainPanel.add(leftPanel, BorderLayout.EAST);
mainPanel.add(rightPanel, BorderLayout.WEST);
mainPanel.add(buttonsPanel, BorderLayout.SOUTH);

leftPanel and rightPanel include what you described.
In this case, each side will get 50% of your screen.

roeygol
  • 4,908
  • 9
  • 51
  • 88
0

GridBagLayout will honour the preferred size of the components where it can, where the available space is less then the preferred size, it will use the minimum size...

You can try giving each column a weightx of 0.5, this will allocate 50% of the remaining available space to each column

Take a closer look at How to Use GridBagLayout for more details

You could split your layout into two sections and use a GridLayout for each column, using individual JPanels with GridBagLayout for the rows, but this will allow the rows to become unaligned.

Another choice is to use something like MigLayout....

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 2
    That won't make the column widths equal; it will only give them equal amounts of any extra space. – VGR Jan 23 '15 at 23:20
  • 2
    Giving all columns a weightx of 1 is identical to giving all columns a weightx of 0.5; in a GridBagLayout, each axis's weights are relative to each other. – VGR Jan 23 '15 at 23:49
0

It is normal for the preferred size of HTML to be one (very wide) line in Swing. And yes, it is frustrating.

Assuming label3 and list3 are the third JLabel and JComboBox, respectively, in the left side of your layout, try this:

htmlLabel.setPreferredSize(
    new Dimension(htmlLabel.getMinimumSize().width,
                  label3.getPreferredSize().height +
                  list3.getPreferredSize().height));

You probably will want to add any vertical insets used in your GridBagLayout between label3 and list3 to the height computation.

Another option would be to use an uneditable JEditorPane in a JScrollPane, instead of a JLabel. That will take care of the wrapping, but getting it to look exactly like a JLabel can be tricky. Setting the font, colors and border may be sufficient on some systems but not enough on others.

VGR
  • 40,506
  • 4
  • 48
  • 63
  • [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer Jan 23 '15 at 23:51
  • I agree in principle that setPreferredSize should be avoided. In this case, I don't know of another way—except of course making a subclass of JLabel for the sole purpose of overriding getPreferredSize(), which doesn't seem to provide any benefit here, unless the HTML label's text is going to change after creation. – VGR Jan 24 '15 at 00:06
  • While I don't have experience with it, I suspect that MigLayout might be a better solution as it's easier to get multiple containers to line up, or so I'm told ;) – MadProgrammer Jan 24 '15 at 00:09