0

My application is constructed as follows:

  • Main window allows user to select CSV file to be parsed
  • JOptionPane appears after a CSV file is selected and the JOptionPane contains a drop-down menu with various choices; each of which generates a separate window
  • Currently, the JOptionPane closes after a selection is made from the menu and the "OK" button is clicked

I am looking for a way to force the JOptionPane to remain open so that the user can select something different if they want. I would like the JOptionPane to be closed only by clicking the "X" in the upper right corner. I am also open to other possibilities to achieve a similar result if using a JOptionPane isn't the best way to go on this.

Here is the relevant block of code I'm working on:

try 
{
    CSVReader reader = new CSVReader(new FileReader(filePath), ',');

    // Reads the complete file into list of tokens.
    List<String[]> rowsAsTokens = null;

    try 
    {
        rowsAsTokens = reader.readAll();
    } 

    catch (IOException e1) 
    {
        e1.printStackTrace();
    }

    String[] menuChoices = { "option 1", "option 2", "option 3" };

    String graphSelection = (String) JOptionPane.showInputDialog(null, 
            "Choose from the following options...", "Choose From DropDown", 
            JOptionPane.QUESTION_MESSAGE, null, 
            menuChoices, // Array of menuChoices
            menuChoices[0]); // Initial choice

    String menuSelection = graphSelection;

    // Condition if first item in drop-down is selected
    if (menuSelection == menuChoices[0] && graphSelection != null)
    {
        log.append("Generating graph: " + graphSelection + newline);

        option1();          
    }

    if (menuSelection == menuChoices[1] && graphSelection != null)
    {

        log.append("Generating graph: " + graphSelection + newline);

        option2();      
    }

    if (menuSelection == menuChoices[2] && graphSelection != null)
    {
        log.append("Generating graph: " + graphSelection + newline);

        option3();
    }

    else if (graphSelection == null)
    {   
        log.append("Cancelled." + newline);
    }
}
THE DOCTOR
  • 4,399
  • 10
  • 43
  • 64
  • Please post your code. – Aubin Feb 11 '13 at 22:03
  • 2
    might be better to put your drop down in a different jframeinstead of an option pane, that will give you more behavior options – Jeff Hawthorne Feb 11 '13 at 22:05
  • I've also noticed that you are comparing `String`s using `==`. This is not the way to do it in `Java`. You should use `equals()` method instead: `menuSelection.equals(menuChoice[0])` – Michael Feb 11 '13 at 23:29
  • @Michael - why is the equals() method preferrable? – THE DOCTOR Feb 12 '13 at 15:15
  • @THEDOCTOR In `Java` the `==` notation is used to compare primitives e.g. `int a = 4; int b = 4; return a == b;` will result in `TRUE`. If you will try to compare two `Objects` like `String`s using `==` it will test if the `Object`s are the same (or, to be precise, it will test if the `reference` is the same). e.g. `String c = "Test"; String d = "Test"; return c == d;` will result in `False`. But `return c.equals(d);` will result in `True`. – Michael Feb 12 '13 at 15:44
  • @THEDOCTOR You are more than welcome. Just out of curiosity, have you solved your original issue? – Michael Feb 12 '13 at 21:23
  • Not yet. I have been looking through everyone's answers and I am still trying to resolve this. – THE DOCTOR Feb 12 '13 at 22:01

3 Answers3

2

I would like for the window with the choices to remain open even after the user has selected an option so that they can select another option if they wish. How do I get the JOptionPane to remain open instead of its default behavior where it closes once a drop-down value is selected?

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

In either of these option panes, I can change my choice as many times as I like before closing it. The 3rd option pane will show (default to) the value selected earlier in the 1st - the current value.

import java.awt.*;
import javax.swing.*;

class Options {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                Object[] options = {
                    "Option 1",
                    "Option 2",
                    "Option 3",
                    "None of the above"
                };
                JComboBox optionControl = new JComboBox(options);
                optionControl.setSelectedIndex(3);
                JOptionPane.showMessageDialog(null, optionControl, "Option",
                        JOptionPane.QUESTION_MESSAGE);
                System.out.println(optionControl.getSelectedItem());

                String graphSelection = (String) JOptionPane.showInputDialog(
                        null,
                        "Choose from the following options...", 
                        "Choose From DropDown",
                        JOptionPane.QUESTION_MESSAGE, null,
                        options, // Array of menuChoices
                        options[3]); // Initial choice
                System.out.println(graphSelection);

                // show the combo with current value!
                JOptionPane.showMessageDialog(null, optionControl, "Option",
                        JOptionPane.QUESTION_MESSAGE);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

I think Michael guessed right with a JList. Here is a comparison between list & combo.

Note that both JList & JComboBox can use a renderer as seen in the combo. The important difference is that a list is an embedded component that supports multiple selection.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"I would like for the window with the choices to remain open"* If by 'window' you mean the option pane - it does stay open. If you mean the combo drop-down, it is a drop-down, **not a window!** – Andrew Thompson Feb 11 '13 at 23:15
1

The following solution won't give you a drop-down menu but it will allow you to select multiple values.

You can use a JList to store your choices and to use JOptionPane.showInputMessage like this:

JList listOfChoices = new JList(new String[] {"First", "Second", "Third"});
JOptionPane.showInputDialog(null, listOfChoices, "Select Multiple Values...", JOptionPane.QUESTION_MESSAGE);

Using the method getSelectedIndices() on listOfChoices after the JOptionPane.showInputDialog() will return an array of integers that contains the indexes that were selected from the JList and you can use a ListModel to get their values:

int[] ans = listOfChoices.getSelectedIndices();
ListModel listOfChoicesModel = listOfChoices.getModel();
for (int i : ans) {
    System.out.println(listOfChoicesModel.getElementAt(i));
}
Michael
  • 1,209
  • 2
  • 12
  • 25
  • As soon as I saw your answer, I heard a penny drop. I think you have got it right for what the OP actually needs. – Andrew Thompson Feb 11 '13 at 23:16
  • @AndrewThompson Thanks :) He wanted a drop down list... It's not the same, but if he doesn't have a lot of values this can do the trick. – Michael Feb 11 '13 at 23:21
  • While this allows the user to select multiple values, the JOptionPane still closes once the "OK" button is clicked. I want the user to continue to have the ability to select items even after the initial selection. – THE DOCTOR Feb 12 '13 at 21:23
  • @THEDOCTOR Yes, the user can select multiple values using the `ctrl` button plus the `left mouse button`. – Michael Feb 12 '13 at 21:27
  • I saw that, but my point it closes after you click OK and I don't want it to close. – THE DOCTOR Feb 12 '13 at 22:03
  • @THEDOCTOR You want your `OK` button to act like an `Apply` button? You can create a custom dialog to achieve this. But the question that arises is: Why do you want this behavior? – Michael Feb 12 '13 at 22:09
  • Currently, the user has to select the CSV file to parse and then the JOptionPane appears. If they want to select a different option, they have to browse for the CSV file all over again and then the JOptionPane will appear again. Each selection launches a statistics graph and they might want to see more than one. I want to allow the user to keep choosing from the available options without having to start over again with browsing for and opening the CSV file. As mentioned, I am open to possibilities other than the JOptionPane if something different would be more suitable. – THE DOCTOR Feb 13 '13 at 14:44
  • @THEDOCTOR Hi, I'm sorry that I didn't reply for your last post. Did you manage to solve it? – Michael Feb 19 '13 at 06:34