0

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);
}}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Noob_Programmer
  • 111
  • 5
  • 14

1 Answers1

2

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);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks for the help @MadProgrammer! That is what I needed! Is there anyway i can change the option names from 1 and 2 to AES and DES? Thanks again! – Noob_Programmer Jan 11 '14 at 07:17
  • Change what you add to the `optionList`, the `JOptionPane` will use the values as the text for the buttons and return you the index of the option that was selected... – MadProgrammer Jan 11 '14 at 07:24
  • ok thank you @MadProgrammer that worked. But it doesnt seem to work for my second menu. I still get AES and DES options instead of CBC and ECB. Can you help me out with that please? Thank you very much! – Noob_Programmer Jan 11 '14 at 07:34