Search code examples
javaswingjlistjdialog

Is there a way to create a JList in a modal dialog?


I have a JFrame already visible. The user can load a saved session.

The idea is to create a JList, so the user can load the chosen session and the frame can be updated.

The code bellow get a list of String and add them to the list.

DefaultListModel model = new DefaultListModel();
JList list=new JList(model);
JScrollPane pane = new JScrollPane(list);
try {
    for (String  part : Utils.getSessions()) {
        model.addElement(part);
    }
} catch (IOException e1) {
    e1.printStackTrace();
}

The next step : display the step.

What I found : add pane to the current frame

My Hope: display the list in a modal dialog

Is there a way to create a JList in a modal dialog?


Solution

  • It turns out that JOptionPane already has list selection built on, no need to work with your own JList.

    Here's the call to use: JOptionPane.showInputDialog

    Here's a trivially simple example: Displaying a dialog with a list of choices

    Here's a fully working example that lets you choose a font name (using the handy-dandy font list snippet given by Andrew in the comments).

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GraphicsEnvironment;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.SwingUtilities;
    import javax.swing.WindowConstants;
    
    public class ListChooserDemo extends JFrame {
      JTextPane textPane = new JTextPane();
      String lastChoice = null;
    
      public ListChooserDemo() {
        setTitle("List Chooser Demo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(new Dimension(500, 500));
        add(new JScrollPane(textPane), BorderLayout.CENTER);
        JPanel buttonPanel = new JPanel(new FlowLayout());
        add(buttonPanel, BorderLayout.SOUTH);
        JButton b = new JButton("Choose it!");
        textPane.setText("Click the button...");
        b.addActionListener(this::doChooseFont);
        buttonPanel.add(b);
      }
    
      public void doChooseFont(ActionEvent e) {
        // a handy way to get a nontrivial list of choices for a demo
        String[] choices = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
    
        // Show a list of options with no effort on our part.
        String input = (String) JOptionPane.showInputDialog(
                this,                         // optional reference to frame/window or null
                "Choose a font...",           // prompt displayed over list
                "Font Chooser",               // title
                JOptionPane.QUESTION_MESSAGE, // message style
                null,                         // Use default icon for message style
                choices,                      // array of choices
                lastChoice);                  // initial choice or null
        if (input == null) {
          // Handle case when user canceled, didn't select anything, or hit escape
          textPane.setText(textPane.getText() + "\r\nCanceled!");
        } else {
          // Do stuff that happens when a selection was made
          textPane.setText(textPane.getText() + "\r\nSelected " + input);
          lastChoice = input;
        }
      }
    
      public static final void main(String[] args) {
        // Run in GUI thread
        SwingUtilities.invokeLater(() -> {
          ListChooserDemo frame = new ListChooserDemo();
          // Center in screen and show
          Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
          frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2);
          frame.setVisible(true);
        });
      }
    }
    

    enter image description here