I have a popup window that instead of the ordinary YES_NO_CANCEL_OPTION I want the user to select from 3 options {A, B, or C}

static int Select_Use_Move_Items(int Select_Col){
int n = JOptionPane.showOptionDialog(frame,
    "Select Option for:\n
       "A: Remove - Discard one Item\n"+
       "B: Transfer one Item to Person\n"+
       "C: Transfer one Item to Transport",
    "Make Selection",
    JOptionPane.YES_NO_CANCEL_OPTION,
    JOptionPane.QUESTION_MESSAGE,
    null,
    options,
    options[2]);
}

How do tell in the calling routine which of the 3 buttons were pressed?
Note: this function is called in 15 different locations.

The popup subroutine is called by:

    int Selection = Main.Select_Use_Move_Items(Col);
    if Selection = 1  {Perform function A}
    if Selection = 2  {Perform function B}
    if Selection = 3  {Perform function C}

Where Col is an integer representing the column in a jtable.

Popup shows up and works but knowing which button that was pressed is what I need to know....
Thanks in advance........ :)

Recommended Answers

well ... if this code is Java, I would suggest to drop this for now, and go over the basics once more.
can you find the obvious mistake you've made here:

int Selection = Main.Select_Use_Move_Items(Col);
    if Selection = 1  {Perform function A}
    if Selection = 2  {Perform …
Jump to Post

so ... did what I added do what you are trying to do?

Jump to Post

actually .... my question was: "did the snippet I posted helped you solve yours?"
instead of the index in an array, it returns the text on the Button that was clicked. isn't that what you are looking for?

Jump to Post
  • use JComboBox, a.m. logics doesn't require waiting for users decision

  • use JOptionPane in the case would need, stop to block code execution,

  • JOptionPane has internal event closed, canceled etc, more in the API

  • return Option_Seleted; probably returns number constant, e.g. button and closed, canceled

Jump to Post

All 13 Replies

well ... if this code is Java, I would suggest to drop this for now, and go over the basics once more.
can you find the obvious mistake you've made here:

int Selection = Main.Select_Use_Move_Items(Col);
    if Selection = 1  {Perform function A}
    if Selection = 2  {Perform function B}
    if Selection = 3  {Perform function C}

?

I also don't understand why you pass that select_col variable, since you don't use it. what exactly is options?

EDIT: even though I'm not entirely sure what you want, I think something like this might be what you're looking for:

static String Select_Use_Move_Items(int Select_Col){
    String[] options = {"Yes","No","CANCEL"};
    int n = JOptionPane.showOptionDialog(null,
        "Select Option for:\n"+
           "A: Remove - Discard one Item\n"+
           "B: Transfer one Item to Person\n"+
           "C: Transfer one Item to Transport",
        "Make Selection",
        JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        null,
        options,
        options[2]);
    return options[n];
    }

Note: the last section of code is pseudo-code.....
that is there go give you some idea of what I want to do

line 1 is the actual function call to activate the popup. the rest is pseudo_code...... I know it is not executable....

The integer Col varible is to tell the popup which column is being effected....
The string displayed for A, B, C options will change due to the value of Col...
I did not include that info because the Popup window works as I need it to and make my post as brief and to the point as possible. I just need to know what button was clicked in the popup window. A B or C.

so ... did what I added do what you are trying to do?

Here is the full and complete code if you have to have it

    static int Select_Use_Move_Items(int Select_Col) {
        String Option_Select = "";
        if (Select_Col == 0)
                Option_Select = 
                    "A: Remove - Use one Item\n"+
                    "B: Transfer one Item to Transport\n"+
                    "C: Transfer one Item to Home Base";
        if (Select_Col == 1)
                Option_Select = 
                    "A: Remove - Use one Item\n"+
                    "B: Transfer one Item to On Person\n"+
                    "C: Transfer one Item to Home Base";
        if (Select_Col == 2)
                Option_Select = 
                    "A: Remove - Use one Item\n"+
                    "B: Transfer one Item to On Person\n"+
                    "C: Transfer one Item to Transport";
         Object[] options = {" A ",
                            " B ",
                            " C "};
            Component frame = null;
            int n = JOptionPane.showOptionDialog(frame,
            "Select Option for:\n"+Option_Select,
            "Make Selection",
        JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        null,
        options,
        options[2]); 
        int Option_Seleted = n;

        return Option_Seleted;

this routine is called by :

Selection = Main.Select_Use_Move_Items(Col);

where Selection (or Option_Selected in the popup routine) is an integer and tells the calling routine which button was pressed

Select_Col tells the popup which choices the user has to choose from, ie column number of a jtable located in another part of the main program.

I hope this will answer your question........

actually .... my question was: "did the snippet I posted helped you solve yours?"
instead of the index in an array, it returns the text on the Button that was clicked. isn't that what you are looking for?

  • use JComboBox, a.m. logics doesn't require waiting for users decision

  • use JOptionPane in the case would need, stop to block code execution,

  • JOptionPane has internal event closed, canceled etc, more in the API

  • return Option_Seleted; probably returns number constant, e.g. button and closed, canceled

Simply return the value of n with a return n; statement at the end of the method.

JamesCherrill: he did return that value, but apparently that doesn't solve his problem. which is a bit where I don't understand it either.

Sorry, I was looking at his orignal post. So is there still a question here, or has it been answered?

that's what I've been trying to find out :)

the value of n when returned for option A is 0 for B and C they are null.
what I need is for A (or YES_OPTION) to return 1 B (or NO_OPTION) to return 2 C (or CANCEL_OPTION) to return 3. or at least something that distinguishes the difference between the 3 options.

I have a possible solution...
Did not want a drop-down-select in the popup, but it does what I need it to do.

    static String Select_Use_Move_Items(int Select_Col) {

        String[]choices = new String [3] ;
        if (Select_Col == 0)
            choices[0] = "A: Remove - Use one Item";
            choices[1] = "B: Transfer one Item to Transport";
            choices[2] = "C: Transfer one Item to Home Base";
        if (Select_Col == 1)
            choices[0] = "A: Remove - Use one Item";
            choices[1] = "B: Transfer one Item to On Person";
            choices[2] = "C: Transfer one Item to Home Base";
        if (Select_Col == 2)
            choices[0] = "A: Remove - Use one Item";
            choices[1] = "B: Transfer one Item to On Person";
            choices[2] = "C: Transfer one Item to Transport";

       String text = (String) JOptionPane.showInputDialog(null, "Choose Option...",
           "Select Action", JOptionPane.QUESTION_MESSAGE, null, 
           choices, choices[0]); 

    return text;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.