Tyilo
Tyilo

Reputation: 30152

BorderLayout not working

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);
    }
}

Upvotes: 5

Views: 11447

Answers (3)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

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.

Upvotes: 9

user802421
user802421

Reputation: 7505

You can try to change

panel.add(cancelButton, BorderLayout.SOUTH);

to

frame.add(cancelButton, BorderLayout.SOUTH);

Result:

enter image description here

Upvotes: 6

Jack
Jack

Reputation: 133639

As Hovercraft Full Of Eels says, JPanel default behavior is the FlowLayout, which is the simplest one, and it's described here. You can easily change it to the manager you need by specifying it inside the constructor:

panel = new JPanel(new BorderLayout())

Upvotes: 3

Related Questions