Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Events on JOptionPane dialogs

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Events on JOptionPane dialogs

    Hi,
    I coded a simple program where the user distributes 60 points to 8 boxes (Box 1 = 12, Box 2 = 7 etc.). After they press the "Done" button, the values will be stored and a dialog box will appear and ask "Are you sure?".
    To this point, everything works, of course. But when I want the program to close if the user presses OK and reset the areas if user says Cancel.

    Unfortunately, I don't seem to find a way to do this. So, how do I add an event listener to JOptionPane? I use JOptionPane.showConfirmDialog() to do this, but I fail.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Events on JOptionPane dialogs

    how do I add an event listener to JOptionPane? I use JOptionPane.showConfirmDialog() to do this, but I fail.
    See the API for JOptionPane
    JOptionPane (Java Platform SE 6)

    No listener necessary, this method returns an integer value corresponding to which selection was chosen (JOptionPane.CANCEL_OPTION, JOptionPane.YES_OPTION, etc...)

  3. The Following User Says Thank You to copeg For This Useful Post:

    beer-in-box (September 25th, 2011)

  4. #3
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Events on JOptionPane dialogs

    Note to self: Read API before asking questions

    But, is it possible you tell me how I use them?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class MyJOption extends JFrame {
    	int c;
    	JButton a;
    	JLabel b;
     
    	public  MyJOption(){
    		super("Title");
    		setLayout(new FlowLayout());
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		a = new JButton("Trying");
    		b = new JLabel("The result of the trial will be shown here");
    		a.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent a){
    				JOptionPane.showMessageDialog(null, "We made the trial", "Başlık", JOptionPane.YES_NO_OPTION);
    				// I GUESS THIS IS WHERE EVENT HANDLING PROCEDURE WILL TAKE PLACE, BUT HOW :)
    			}
    		});
     
     
    	}
    }
    I am just so confused about it. I tried to declare an int to handle it, but no luck.

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Events on JOptionPane dialogs

    The following link should have code examples for you to use.
    How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Just check the returned int against the values I posted above (eg JOptionPane.YES_OPTION, etc..)

  6. #5
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Events on JOptionPane dialogs

    Hey, long time passed. Because I was trying to understand. But unfortunately, I couldn't.
    I tried to get the returned int, but I got 1 and zero. No YES_OPTION.

    I have seen many examples about how to do that. But they all created a custom dialog and this is not what I want. I want to get the user choice from the default JOptionPane.

    Can you provide an example about that or guide me a little more, please? I am totally lost here.

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Events on JOptionPane dialogs

    Quote Originally Posted by beer-in-box View Post
    I tried to get the returned int, but I got 1 and zero. No YES_OPTION.
    Again, I will point you back to the API - JOptionPane.NO_OPTION is defined as 1...and I will leave it up to you to look up what JOptionPane.YES_OPTION is defined as (hint: the other value you are receiving). The page I linked to above has demo code...all you need is to check that the returned API is equal to the values you expect (NO_OPTION, etc...)

  8. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: Events on JOptionPane dialogs

    Quote Originally Posted by copeg View Post
    Again, I will point you back to the API - JOptionPane.NO_OPTION is defined as 1...and I will leave it up to you to look up what JOptionPane.YES_OPTION is defined as (hint: the other value you are receiving). The page I linked to above has demo code...all you need is to check that the returned API is equal to the values you expect (NO_OPTION, etc...)
    int myConfirmDialog = JOptionPane.showConfirmDialog(all your parameters go here);

    now, this integer "myConfirmDialog" stores a number which corresponds to the button clicked. the first button will be numbered 0, the second will be numbered 1, the third will be numbered 2 and so on. if you click the first button, myConfirmDialog will have the value 0. using if...else you can add event handling.

Similar Threads

  1. [SOLVED] events log
    By nasi in forum Java Theory & Questions
    Replies: 1
    Last Post: May 17th, 2010, 05:21 AM
  2. Handling two button events?
    By BelowTheHeavens in forum AWT / Java Swing
    Replies: 2
    Last Post: May 15th, 2010, 01:35 AM
  3. JComboBoxes & Events
    By KevinGreen in forum AWT / Java Swing
    Replies: 3
    Last Post: April 21st, 2010, 05:11 AM
  4. Connecting events
    By Olufemi in forum AWT / Java Swing
    Replies: 0
    Last Post: April 21st, 2010, 04:58 AM
  5. 'MouseUp' Drag and Drop Events
    By copeg in forum AWT / Java Swing
    Replies: 0
    Last Post: November 10th, 2009, 07:21 PM