This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Use JOptionPane#showConfirmDialog() or JOptionPane#showInputDialog() or JOptionPane#showOptionDialog() All these return some value which indicates the user choice.
Thanks its working, iv found JOptionPane.OK_OPTION == 0 in a java book is it better to stick to the one you showed me?? Or doesnt it really matter?? thanks
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
> Is there a way to do this with the standard JOptionPane.showMessageDialog
is there a particular reason for adding an actionListener to the OK button?
you could put the code you plan for the actionEvent immediately after showing the optionPane, and it will be executed as soon as the optionPane closes, but the user can still close the optionPane via 'X' and the 'esc' key, so you may not want the code executed if the optionPane is closed this way.
if what you do is based on user selection, or how user closes the optionPane, the other optionPanes are better
Maja Gralewska
Ranch Hand
Joined: Sep 26, 2008
Posts: 92
posted
0
hey,
It doesn't have be be an action listener, but i do want to be able to distinguish between the OK button and the X button, because they both return a value of 0 when pressed. Is there a another way to distinguish between the X and OK button or must i just create a custom one?? I only want one button the OK button.
i do want to be able to distinguish between the OK button and the X button, because they both return a value of 0 when pressed.
Nope. Read the API. When one of the showXxxDialog methods returns an integer, the possible values are:
YES_OPTION
NO_OPTION
CANCEL_OPTION
OK_OPTION
CLOSED_OPTION
Note that OK_OPTION and CLOSED_OPTION are separate, different values. And use the JOptionPane constants, not a magic number, to identify the user action.
db
luck, db
There are no new questions, but there may be new answers.
Maja Gralewska
Ranch Hand
Joined: Sep 26, 2008
Posts: 92
posted
0
hi
The code i used was: JOptionPane.showMessageDialog(this, "Edit DA Code!");
if (JOptionPane.OK_OPTION == 0) { // Action i want done }
When i press ok it goes into the if and does the code, but when i press X it also goes into the if and does the code, can you please explain why it does this?
thanks
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
you do understand that if (JOptionPane.OK_OPTION == 0)
will always be true (unless sun changes it later), because JOptionPane.OK_OPTION is 0
from the source code: public static final int OK_OPTION = 0;
if you need to distinguish between how the optionpane closes, don't use messageDialog