1
\$\begingroup\$

I have this code:

JPanel controlPanel = new JPanel();
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.PAGE_AXIS));
TitledBorder tb2 = BorderFactory.createTitledBorder(null, "Control Panel", 
        TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION, myFont, new Color(0, 153, 0));
controlPanel.setBorder(tb2);

JPanel fromDate = new JPanel();
fromDate.setLayout(new BoxLayout(fromDate, BoxLayout.X_AXIS));
fromDate.add(new JLabel("From date: "));
JButton fromDateButton = new JButton("...");
fromDateButton.setMaximumSize(new Dimension(100,15));
fromDate.add(fromDateButton);
JPanel toDate = new JPanel();
toDate.setLayout(new BoxLayout(toDate, BoxLayout.X_AXIS));
toDate.add(new JLabel("Until date: "));
JButton toDateButton = new JButton("...");
toDateButton.setMaximumSize(new Dimension(100,15));
toDate.add(toDateButton);
controlPanel.add(Box.createRigidArea(new Dimension(0,10)));
controlPanel.add(fromDate);
controlPanel.add(Box.createRigidArea(new Dimension(0,10)));
controlPanel.add(toDate);
toDate.setAlignmentX(Component.LEFT_ALIGNMENT);
fromDate.setAlignmentX(Component.LEFT_ALIGNMENT);

gui.add(controlPanel, BorderLayout.WEST);

Which produce this GUI (The relevant part is shown):

enter image description here

I want to have a label and a button right next to it. There should be a better way to achieve this. Any suggestions?

\$\endgroup\$
  • 1
    \$\begingroup\$ BoxLayout accepting min, max and preferred size, override these coordinates, but GBC is easier \$\endgroup\$ – mKorbel Jun 10 '13 at 12:44
4
\$\begingroup\$

When you create a form, a GridBagLayout is usually needed.

Here's one way to create the form in your question.

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SimpleGridBagLayout implements Runnable {

    @Override
    public void run() {
        JFrame frame = new JFrame("GridBagLayout Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.gridwidth = 2;
        gbc.gridx = 0;
        gbc.gridy = 0;

        JLabel titleLabel = new JLabel("Control Panel");
        panel.add(titleLabel, gbc);

        gbc.anchor = GridBagConstraints.LINE_START;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 1;
        gbc.gridy++;

        JLabel fromDateLabel = new JLabel("From date:");
        panel.add(fromDateLabel, gbc);

        gbc.gridx++;

        JButton fromDateButton = new JButton("...");
        panel.add(fromDateButton, gbc);

        gbc.gridx = 0;
        gbc.gridy++;

        JLabel toDateLabel = new JLabel("To date:");
        panel.add(toDateLabel, gbc);

        gbc.gridx++;

        JButton toDateButton = new JButton("...");
        panel.add(toDateButton, gbc);

        frame.add(panel);
        frame.setSize(250, 200);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new SimpleGridBagLayout());
    }

}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks. Is there a way to resize the buttons? (Using setpreferredsize or setMaximumSize doesn't work for this Layout). \$\endgroup\$ – Maroun Jun 6 '13 at 6:56
  • \$\begingroup\$ @Maroun Maroun: The horizontal fill should ensure that the buttons are the same size. \$\endgroup\$ – Gilbert Le Blanc Jun 6 '13 at 6:59
  • \$\begingroup\$ Indeed. But I want then to be smaller than the size the horizontal fill makes. (Look at the image in my question) \$\endgroup\$ – Maroun Jun 6 '13 at 7:06
1
\$\begingroup\$

Add a glue in x 3 to get the extra space and then buttons won't get bigger.

panel.add(Box.createGlue(), gbc);
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.