Search code examples
javaswingjoptionpane

JOptionPane input dialog menu


Looking for some help with my code here. I'm trying to create a JOptionPane input dialog that will take the input from the user (either option 1 or option 2) and display the next menu based on the first input. There will be a different outcome for 1 and 2.

Am I going about this the right way?

Code:

public class MyJavaApplication {
    public static void main(String[] args) throws FileNotFoundException {
        //1. Options

        List<String> optionList = new ArrayList<String>(); **//Create an array to store user input**
        optionList.add("1");
        optionList.add("2");

        Object[] options = optionList.toArray(); **//Store option into array**

        Object value = JOptionPane.showInputDialog(null, 
            "Please select your encryption Algorithm:(1 or 2)\n 1. Data Encryption       Standard(DES) \n 2. Advanced Encryption Standard(AES)", 
             null, 
             options, 
             options[0], 
             options[1]); **//JOption input dialog asking for either option one or 2**

        int index = optionList.indexOf(value);

Edit:

if (value == 1) {
    List<String> optionList2 = new ArrayList<String>();
        optionList2.add("ECB");
        optionList2.add("CBC");

        Object[] options2 = optionList2.toArray();

        int value2 = JOptionPane.showOptionDialog(null, 
        "Please select your mode of operation:\n 1. Cipher Block Chaining(CBC) \n 2. Electronic Codebook(ECB)", 
        "Select",
        JOptionPane.YES_NO_OPTION,
        JOptionPane.QUESTION_MESSAGE, 
        null,
        options, 
        optionList2.get(0));

        String option2 = optionList2.get(value2);
}}

Solution

  • Not sure why you're trying to use a input dialog. A input dialog will return the value entered into the input field or null if the dialog was dismissed, this doesn't seem to be what you really want...

    Instead, try using a standard input dialog....

    Options

    List<String> optionList = new ArrayList<String>();
    optionList.add("1");
    optionList.add("2");
    
    Object[] options = optionList.toArray();
    int value = JOptionPane.showOptionDialog(
                    null,
                    "Please select your encryption Algorithm:(1 or 2)\n 1. Data Encryption       Standard(DES) \n 2. Advanced Encryption Standard(AES)",
                    "Pick",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    options,
                    optionList.get(0));
    
    String opt = optionList.get(value);
    System.out.println("You picked " + opt);