Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Message Dialog box --> action performed.

 
Maja Gralewska
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey

How to i add an action lister to the OK button in a standard message dialog box??

JOptionPane.showMessageDialog(null, "Please complete the following required fields: ");

I know how to add an action lister to a custom dialog eg:

String[] question = {"Java", "C#"};

int response = JOptionPane.showOptionDialog(null, "Pick java, Pick java", "question", 0, JOPtionPane>QUESTION_MESSAGE, null, question, question[0]);

// Action

if (response == 0) {
// Action listener here
}

Is there a way to do this with the standard JOptionPane.showMessageDialog or do i have to create a custom one to use it in this(or similar) way??

thanks
 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use JOptionPane#showConfirmDialog() or JOptionPane#showInputDialog() or JOptionPane#showOptionDialog()
All these return some value which indicates the user choice.
 
Maja Gralewska
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    Maja Gralewska
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 4632
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Maja Gralewska
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic