4

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

m ainsworth
  • 49
  • 1
  • 6

1 Answers1

5

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

Peter
  • 1,592
  • 13
  • 20
  • Was pretty much what I needed and with a few tweaks like text size it does the job. Thanks. – m ainsworth Jun 05 '16 at 07:24
  • Just discovered an error; how do I pick up the cancel option. Something like dialog.setOnClose(e -> goto end) to go between the dialog.setOnOk and dialog.show. In one place once the user has picked an item from the list there is further code. Or do I have to drop the value into a string and check for a null. – m ainsworth Jun 10 '16 at 15:16
  • The setOnOk/setOnClose calls determine what should happen when those buttons are clicked. They don't execute immediately. As long as you set them before you display the dialog it should be fine, as there wont be an opportunity to click the button before it has any functionality – Peter Jun 10 '16 at 15:44
  • ta, can sort it from there. – m ainsworth Jun 10 '16 at 16:59