How to add ActionListener to the button on JOptionPane?

Posted on 2002-06-12
Last Modified: 2010-03-31
 I want to add my ActionListener to the buttons on the JOptionPane. And I don't want the message box disappear after I press any buttons if my validation checking return false. Thank you.
Question by:JavaSlave
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 92

Expert Comment

ID: 7074898
Don't use the convience method, and instead construct the JOptionPane contents yourself.

Author Comment

ID: 7079955
Hi Objects,
  Can you tell me more about that ? Bcos of I've no idea how to do it..

Thank you .
LVL 92

Accepted Solution

objects earned 20 total points
ID: 7079963
Have a look at the javadoc.

To create and use an JOptionPane directly, the standard pattern is roughly as follows:
     JOptionPane pane = new JOptionPane(arguments);
     pane.set.Xxxx(...); // Configure
     JDialog dialog = pane.createDialog(parentComponent, title);;

Here's the javadoc for one of the ctor's mentioning that you can pass buttons in the options array:

public JOptionPane(Object message,
                   int messageType,
                   int optionType,
                   Icon icon,
                   Object[] options)Creates an instance of JOptionPane to display a message with the specified message type, icon, and options. None of the options is initially selected.
The options objects should contain either instances of Components, (which are added directly) or Strings (which are wrapped in a JButton). If you provide Components, you must ensure that when the Component is clicked it messages setValue in the created JOptionPane.

LVL 35

Expert Comment

ID: 8860379
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this
question is:

- points to objects

Please leave any comments here within the
next seven days.


Cleanup Volunteer

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question