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.
Re: Events on JOptionPane dialogs
Quote:
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...)
Re: Events on JOptionPane dialogs
Note to self: Read API before asking questions :)
But, is it possible you tell me how I use them?
Code :
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.
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..)
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.
Re: Events on JOptionPane dialogs
Quote:
Originally Posted by
beer-in-box
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...)
Re: Events on JOptionPane dialogs
Quote:
Originally Posted by
copeg
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.