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