JoptionPane multi select and srollable option

时光毁灭记忆、已成空白 提交于 2019-12-31 04:07:18

问题


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);


来源:https://stackoverflow.com/questions/27379663/joptionpane-multi-select-and-srollable-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!