Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Message Dialog box --> action performed. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Message Dialog box --> action performed." Watch "Message Dialog box --> action performed." New topic
Author

Message Dialog box --> action performed.

Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
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


Potrzeba jest matka wynalazków
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Use JOptionPane#showConfirmDialog() or JOptionPane#showInputDialog() or JOptionPane#showOptionDialog()
All these return some value which indicates the user choice.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
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
> 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
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

Joined: May 03, 2008
Posts: 4642
    
    5

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
    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
    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

    Joined: Sep 26, 2008
    Posts: 92
    Thanks
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Message Dialog box --> action performed.