import com.sun.java.swing.*; import java.awt.*; import java.awt.event.*; /* * XXX: This class is implemented as a weird mix of class and instance * methods/variables. I should clean up the API. */ public class TableDialog extends JDialog { static TableDialog dialog; static String value = ""; JList list; public static void showDialog(Component comp, String newValue, Object[] possibleValues, String title) { if (dialog == null) { Frame frame = JOptionPane.getFrameForComponent(comp); dialog = new TableDialog(frame, possibleValues, title); //XXX: should create new dialog if possibleValues //XXX: isn't the same as before. Or just change the UI. } value = newValue; dialog.list.setSelectedValue(value, true); dialog.setLocationRelativeTo(comp); dialog.setVisible(true); } public static String getValue() { return value; } private TableDialog(Frame frame, Object[] data, String title) { super(frame, title, true); //buttons JButton cancelButton = new JButton("Cancel"); final JButton setButton = new JButton("Set"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { TableDialog.dialog.setVisible(false); } }); setButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { TableDialog.value = (String)(list.getSelectedValue()); TableDialog.dialog.setVisible(false); } }); //main part of the dialog list = new JList(data); list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { setButton.doClick(); } } }); JScrollPane listScroller = new JScrollPane(list); listScroller.setPreferredSize(new Dimension(250, 80)); //XXX: Must do the following, too, or else the scroller thinks //XXX: it's taller than it is: listScroller.setMinimumSize(new Dimension(250, 80)); listScroller.setAlignmentX(LEFT_ALIGNMENT); //Create a container so that we can add a title around //the scroll pane. Can't add a title directly to the //scroll pane because its background would be white. //Lay out the label and scroll pane from top to button. JPanel listPane = new JPanel(); listPane.setLayout(new BoxLayout(listPane, BoxLayout.Y_AXIS)); JLabel label = new JLabel("Pick a last name:"); listPane.add(label); listPane.add(Box.createRigidArea(new Dimension(0,5))); listPane.add(listScroller); listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); //Lay out the buttons from left to right. JPanel buttonPane = new JPanel(); buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS)); buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); buttonPane.add(Box.createHorizontalGlue()); buttonPane.add(cancelButton); buttonPane.add(Box.createRigidArea(new Dimension(10, 0))); buttonPane.add(setButton); //Put everything together, using the content pane's BorderLayout. Container contentPane = getContentPane(); contentPane.add(listPane, BorderLayout.CENTER); contentPane.add(buttonPane, BorderLayout.SOUTH); pack(); } }