JoptionPane multi select and srollable option

2020-04-30 03:48发布

问题:

I'm new to JoptionPane is there any method that i can have multi select and scrollable feature. Please find my code below.

String bigList[] = new String[100];

        for (int i = 0; i < bigList.length; i++) {
          bigList[i] = Integer.toString(i);
        }

for scrolling i'm using

 JOptionPane.showInputDialog(new JFrame(), "Pick a printer", "Input", JOptionPane.QUESTION_MESSAGE,null, bigList, "Titan");

for multi select im using

 JList list = new JList(bigList);
        JOptionPane.showMessageDialog(null, list, "Select Test Case (For Multiple Selections Press 'Ctrl') ", JOptionPane.PLAIN_MESSAGE);

problem is i need to combine both features i.e., both scrolling and multi select options.Can any one provide me a proper code.

回答1:

I'm new to JoptionPane is there any method that i can have multi select and scrollable feature. Please find my code below.

  • based on Oracle tutorial How to Make Dialogs - Getting the User's Input from a Dialog

  • Swing JComponents (its models) are designated to works with standard Java data types

  • for example, there are two models (react to numbers 0-9 from keyboard)

import java.awt.EventQueue;  
import javax.swing.Icon;  
import javax.swing.JOptionPane;  
import javax.swing.UIManager;  

public class MyOptionPane {  

    public MyOptionPane() {  
        Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");  
        Object[] possibilities = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};  
        Integer i = (Integer) JOptionPane.showOptionDialog(null,   
                null,  "ShowInputDialog",   
                JOptionPane.PLAIN_MESSAGE, 1,  errorIcon, possibilities, 0);

        // or

        Integer ii = (Integer) JOptionPane.showInputDialog(null,  
                "Select number:\n\from JComboBox", "ShowInputDialog",  
                JOptionPane.PLAIN_MESSAGE, errorIcon, possibilities, "Numbers");  
    }  

    public static void main(String[] args) {  
        EventQueue.invokeLater(new Runnable() {  
            @Override  
            public void run() {  
                MyOptionPane mOP = new MyOptionPane();  
            }  
        });  
    }  
}


回答2:

Got the answer

JList list = new JList(bigList);
          JScrollPane jscrollpane=new JScrollPane();
          jscrollpane.setViewportView(list);

            JOptionPane.showMessageDialog(null, jscrollpane, "Select Value", JOptionPane.PLAIN_MESSAGE);