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):
I want to have a label and a button right next to it. There should be a better way to achieve this. Any suggestions?