1

I am trying to create a JDialog that looks like this:

enter image description here

but where the JTextField spans two columns so that it almost reaches the "Find Next" button. However, when I try to set the gridwidth of the JTextField to 2, I get this:

enter image description here

Here is my code:

JDialog dialog = new JDialog(frame, "Find");
dialog.setPreferredSize(new Dimension(370, 129));
Point p = frame.getLocation();
dialog.setLocation(p.x + 53, p.y + 170);

Container c = dialog.getContentPane();
GridBagLayout gbl = new GridBagLayout();
c.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();

JLabel label = new JLabel("Find what:");
gbc.gridx = gbc.gridy = 0;
c.add(label, gbc);

JTextField field = new JTextField();
field.setMinimumSize(field.getPreferredSize());
gbc.gridx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
c.add(field, gbc);

JButton findNext = new JButton("Find Next");
findNext.setPreferredSize(new Dimension(82, 23));
gbc.gridx = 3;
c.add(findNext, gbc);

JCheckBox check = new JCheckBox("Match case");
gbc.gridx = 0;
gbc.gridy = 1;
c.add(check, gbc);

JRadioButton up = new JRadioButton("Up"), down = new JRadioButton("Down");
ButtonGroup group = new ButtonGroup();
group.add(up);
group.add(down);
gbc.gridx = 1;
c.add(up, gbc);
gbc.gridx = 2;
c.add(down, gbc);

JButton cancel = new JButton("Cancel");
cancel.setPreferredSize(new Dimension(82, 23));
cancel.addActionListener(e2 -> dialog.dispose());
gbc.gridx = 3;
c.add(cancel, gbc);

Font f = new Font("Segoe UI", Font.PLAIN, 12);
c.setFont(f);
for (Component child : c.getComponents())
    child.setFont(f);

dialog.pack();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setResizable(false);
dialog.setVisible(true);

Thanks in advance!

Cg2916
  • 1,117
  • 6
  • 23
  • 36
  • 1
    This `dialog.setPreferredSize(new Dimension(370, 129));` is a bad idea, it is directly related to your particular issue, but as a side note, all the `setPreferredSize` calls are a problem. Instead, get rid of them and let the layout manager do it's job in combination with pack – MadProgrammer Jan 31 '15 at 06:17

1 Answers1

2

So two problems...

  1. You set the gridwidth to 2, but never reset it: gbc.gridwidth = 2;, this means that all subsequent uses of the GridBagConstraints has a gridwidth of 2
  2. Overuse of setPreferredSize, in fact, any use is overuse...

Solution...

Rest the gridwidth before re-using the GridBagConstraints...

gbc.gridwidth = 2;
c.add(field, gbc);

gbc.gridwidth = 1;
//...

Don't use setPreferredSize, you don't control the properties which can change the required size for components across multiple platforms.

See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more discussions

With setPreferredSize:

enter image description here

Without setPrefferedSize:

enter image description here

Can you see the difference between the buttons?

If you want to add padding into a component use an EmptyBorder and/or GridBagConstraints#insets for starters

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366