I am trying to create a JDialog that looks like this:
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:
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!