import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SelectionDialog extends JDialog { private JPanel panel; private ButtonGroup group; public SelectionDialog(JFrame parent, String title, String msg) { super(parent, title, true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { setVisible(false); } }); panel = new JPanel() { public Insets getInsets() { return new Insets(10, 10, 10, 10); } }; panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); JLabel label = new JLabel(msg); // label.setAlignmentX(CENTER_ALIGNMENT); panel.add(label); getContentPane().add(panel, BorderLayout.CENTER); group = new ButtonGroup(); JPanel bPanel = new JPanel() { public Insets getInsets() { return new Insets(10, 10, 10, 10); } }; bPanel.setLayout(new FlowLayout()); JButton button = new JButton("OK"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); } }); bPanel.add(button); getContentPane().add(bPanel, BorderLayout.SOUTH); pack(); } void addItem(String text) { JRadioButton rb = new JRadioButton(text); panel.add(rb); group.add(rb); // if (!group.isSelected()) rb.setSelected(true); pack(); } public void pack() { super.pack(); // center dialog in parent Rectangle r = getParent().getBounds(), r1 = getBounds(); setLocation(r.x + (r.width - r1.width) / 2, r.y + (r.height - r1.height) / 2); } /* public boolean wasClosed() { return buttonPressed; } */ }