Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to add ActionListener to the button on JOptionPane?

Posted on 2002-06-12
5
Medium Priority
?
787 Views
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.
0
Comment
Question by:JavaSlave
  • 2
4 Comments
 
LVL 92

Expert Comment

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

Author Comment

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

Thank you .
0
 
LVL 92

Accepted Solution

by:
objects earned 80 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);
     dialog.show();


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.


0
 
LVL 35

Expert Comment

by:girionis
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses

824 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