How to get the value of option selected from JOptionPane.showMessageDialog?

user3529351

I am making a java game. This is a piece of my code in which I have a question.

How to get the value (no matter index value or string value) of option selected? I have to use it later in my code, for instance if(n="5 point game") { do this}

newgame=new JButton("NEW GAME");
newgame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
      Object[] options = {"5 point game",
            "Non stop game"};
      int n = JOptionPane.showOptionDialog(newgame,
                                           "Choose a mode to play ",
                                           "New Game",
                                            JOptionPane.YES_NO_OPTION,
                                            JOptionPane.QUESTION_MESSAGE,
                                            null,     //do not use a custom Icon
                                            options,  //the titles of buttons
                                            options[0]); //default button title
}
});
msrd0

Your options must be sorted as the buttons will be without your custom options. In your case, this means that options[0] is mapped to JOptionPane.YES_OPTION and options[1] to JOptionPane.NO_OPTION.

For your code, you can use an if-statement like this:

if (n == JOptionPane.YES_OPTION) {
    // 5 point game
}
else if (n == JOptionPane.NO_OPTION) {
    // non stop game
}
else {
    // the user closed the dialog without clicking an button
}

See also this example.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the selected option value from select inside div

From Dev

Get selected option value from specific select

From Dev

How to get the option value of selected in Select dropdown

From Dev

How to get selected option value into PHP

From Dev

How to get option value of only the selected checkboxes?

From Dev

How to get the value of option with attribute selected

From Dev

how to get the selected option in value in php

From Dev

Get Selected Option by Value

From Dev

How to position JOptionPane.showMessageDialog at the center of screen?

From Dev

How to position JOptionPane.showMessageDialog at the center of screen?

From Dev

How to display System.out.print from a function to JOptionPane.showMessageDialog?

From Dev

How to get selected option

From Dev

Magento : How to get selected bundle option title and value from bundle product?

From Dev

How do I get the value from a data-selected input option?

From Dev

Magento : How to get selected bundle option title and value from bundle product?

From Dev

How to get a value, using python, from a selected option in a drop down list in HTML? (FLASK)

From Dev

get selected value from option tag and store in db

From Dev

How to get the value of a selected option and save it into a variable? (Javascript)

From Dev

How to get the value of a selected option and save it into a variable? (Javascript)

From Dev

How to get the value of a selected option dynamically and store it into a variable?

From Dev

How to get selected option value of all rows of table in JavaScript?

From Dev

Get the text from a selected option

From Dev

Get text from selected option

From Dev

Displaying a array using showMessageDialog and return selected value

From Dev

Java Swing: Get text value from JOptionPane

From Dev

How to get selected value from xml?

From Dev

how to get key of selected value from nsdictionary

From Dev

How to remove selected GET value from a URL

From Dev

AngularJS how to get label and value from selected

Related Related

  1. 1

    How to get the selected option value from select inside div

  2. 2

    Get selected option value from specific select

  3. 3

    How to get the option value of selected in Select dropdown

  4. 4

    How to get selected option value into PHP

  5. 5

    How to get option value of only the selected checkboxes?

  6. 6

    How to get the value of option with attribute selected

  7. 7

    how to get the selected option in value in php

  8. 8

    Get Selected Option by Value

  9. 9

    How to position JOptionPane.showMessageDialog at the center of screen?

  10. 10

    How to position JOptionPane.showMessageDialog at the center of screen?

  11. 11

    How to display System.out.print from a function to JOptionPane.showMessageDialog?

  12. 12

    How to get selected option

  13. 13

    Magento : How to get selected bundle option title and value from bundle product?

  14. 14

    How do I get the value from a data-selected input option?

  15. 15

    Magento : How to get selected bundle option title and value from bundle product?

  16. 16

    How to get a value, using python, from a selected option in a drop down list in HTML? (FLASK)

  17. 17

    get selected value from option tag and store in db

  18. 18

    How to get the value of a selected option and save it into a variable? (Javascript)

  19. 19

    How to get the value of a selected option and save it into a variable? (Javascript)

  20. 20

    How to get the value of a selected option dynamically and store it into a variable?

  21. 21

    How to get selected option value of all rows of table in JavaScript?

  22. 22

    Get the text from a selected option

  23. 23

    Get text from selected option

  24. 24

    Displaying a array using showMessageDialog and return selected value

  25. 25

    Java Swing: Get text value from JOptionPane

  26. 26

    How to get selected value from xml?

  27. 27

    how to get key of selected value from nsdictionary

  28. 28

    How to remove selected GET value from a URL

  29. 29

    AngularJS how to get label and value from selected

HotTag

Archive