Search code examples
javadialogjlistnetbeans-8

Java Dialog with jList and option buttons


I would like a Dialog box that contains a JList for user selection. Whilst the following will do this I would also like a message and a 'cancel' button.

list = new JList(LstArray1.toArray());
JOptionPane.showMessageDialog(
 null, list, "Title", JOptionPane.INFORMATION_MESSAGE);

So more like this but changing the combobox to the JList.

String input = (String) JOptionPane.showInputDialog (null, "Choose from list", "title", JOptionPane.INFORMATION_MESSAGE, null, LstArray2.toArray(), LstArray2.get(0));

I've looked at the following but cannot seem to find quite what I need. http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html


Solution

  • This mightn't be exactly what you're looking for, but hopefully it will provide a basis for what you need or spark an alternative approach of your own:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class ListDialog {
        private JList list;
        private JLabel label;
        private JOptionPane optionPane;
        private JButton okButton, cancelButton;
        private ActionListener okEvent, cancelEvent;
        private JDialog dialog;
    
        public ListDialog(String message, JList listToDisplay){
            list = listToDisplay;
            label = new JLabel(message);
            createAndDisplayOptionPane();
        }
    
        public ListDialog(String title, String message, JList listToDisplay){
            this(message, listToDisplay);
            dialog.setTitle(title);
        }
    
        private void createAndDisplayOptionPane(){
            setupButtons();
            JPanel pane = layoutComponents();
            optionPane = new JOptionPane(pane);
            optionPane.setOptions(new Object[]{okButton, cancelButton});
            dialog = optionPane.createDialog("Select option");
        }
    
        private void setupButtons(){
            okButton = new JButton("Ok");
            okButton.addActionListener(e -> handleOkButtonClick(e));
    
            cancelButton = new JButton("Cancel");
            cancelButton.addActionListener(e -> handleCancelButtonClick(e));
        }
    
        private JPanel layoutComponents(){
            centerListElements();
            JPanel panel = new JPanel(new BorderLayout(5,5));
            panel.add(label, BorderLayout.NORTH);
            panel.add(list, BorderLayout.CENTER);
            return panel;
        }
    
        private void centerListElements(){
            DefaultListCellRenderer renderer = (DefaultListCellRenderer) list.getCellRenderer();
            renderer.setHorizontalAlignment(SwingConstants.CENTER);
        }
    
        public void setOnOk(ActionListener event){ okEvent = event; }
    
        public void setOnClose(ActionListener event){
            cancelEvent  = event;
        }
    
        private void handleOkButtonClick(ActionEvent e){
            if(okEvent != null){ okEvent.actionPerformed(e); }
            hide();
        }
    
        private void handleCancelButtonClick(ActionEvent e){
            if(cancelEvent != null){ cancelEvent.actionPerformed(e);}
            hide();
        }
    
        public void show(){ dialog.setVisible(true); }
    
        private void hide(){ dialog.setVisible(false); }
    
        public Object getSelectedItem(){ return list.getSelectedValue(); }
    }
    

    example usage:

    JList list = new JList(new String[] {"foo", "bar", "foobar"});
            ListDialog dialog = new ListDialog("Please select an item in the list: ", list);
            dialog.setOnOk(e -> System.out.println("Chosen item: " + dialog.getSelectedItem()));
            dialog.show();
    

    Feel free to use/modify the above and if you have any questions ask below