BorderLayout not working

Tyilo picture Tyilo · Jul 30, 2011 · Viewed 8.2k times · Source

I cannot get BorderLayout to work. I want the cancelbutton to be positioned at the bottom, but it doesn't work. Code:

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonModel;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

class Test {
    public static JFrame owner;
    public static void main(String[] args) {
        final JDialog frame = new JDialog(owner, "Test");
        frame.setLayout(new BorderLayout());
        frame.setSize(500, 300);
        final JPanel panel = new JPanel();
        final ButtonGroup group = new ButtonGroup();
        String[] options = {"1", "2", "3"};
        for (String text : options) {
            JRadioButton option = new JRadioButton(text);
            option.setActionCommand(text);
            group.add(option);
            panel.add(option);
        }
        JButton okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ButtonModel selectedModel = group.getSelection();
                if (selectedModel != null) {
                    System.err.println(selectedModel.getActionCommand());
                }
            }
        });
        panel.add(okButton);
        JButton cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
                frame.dispose();
            }
        });
        panel.add(cancelButton, BorderLayout.SOUTH);
        frame.add(panel);
        frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }
}

Answer

Hovercraft Full Of Eels picture Hovercraft Full Of Eels · Jul 30, 2011

You add cancelButton to panel using the BorderLayout.SOUTH constant:

  panel.add(cancelButton, BorderLayout.SOUTH);

But where do you set panel's layout to be BorderLayout? Since you never set this container's layout it will use the default layout for JPanel which is FlowLayout.

Solution: set the panel JPanel's layout to BorderLayout to get BorderLayout behavior.

Once you solve this, you'll have another problem though:

  for (String text : options) {
     JRadioButton option = new JRadioButton(text);
     option.setActionCommand(text);
     group.add(option);
     panel.add(option);
  }

You're adding JRadioButton's to the same panel JPanel without regard to layout. I suspect that you want to add the JRadioButtons to their own JPanel, probably one that uses GridLayout(1, 0) or GridLayout(0, 1), depending on desired orientation, and then that you want to add this JPanel to panel, perhaps in the BorderLayout.CENTER position.

Also you have a similar problem with your okButton in that you add it to panel without regard to layout.