How can I know which button is clicked in JDialog ShowModal  
Author Message
Angry Orca





PostPosted: 2004-8-18 7:16:00 Top

java-programmer, How can I know which button is clicked in JDialog ShowModal Hi, all,
In a programme using swing (homework), I use a JDialog window. There are
two buttons on this dialog window. I want to write the program in this way:

// some thing
JDialog d = new ...
...
d.setModal(true);
d.show();
if (d.close = btnOk){
...
} else if (d.close == btnCancel){
...
}

In fact I want to use this way like in the windows programming:
int i = d.showModal();
if (i == ok){
...
} else if ( i == cancel){
...
}

Please help me. I am a newbie in swing programming.

Thanks a lot.
 
tor.iver.wilhelmsen





PostPosted: 2004-8-18 16:11:00 Top

java-programmer >> How can I know which button is clicked in JDialog ShowModal Angry Orca <email***@***.com> writes:

> In fact I want to use this way like in the windows programming:
> int i = d.showModal();
> if (i == ok){
> ...
> } else if ( i == cancel){
> ...
> }

Make your own subclass of JDialog that has such a method, and return a
value from it that you test. Preferrably against a constant like an
int, and not against a component. Example:

public class MyDialog extends JDialog {

public static final int OK = 1;
public static final int CANCEL = 2;
int returnValue = 0;

JButton okButton = new JButton();
...

okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
returnValue = OK;
hide();
}
}

public int showModal() {
setModal(true);
show();
return returnValue;
}
}
 
Luke





PostPosted: 2004-8-18 21:04:00 Top

java-programmer >> How can I know which button is clicked in JDialog ShowModal
>
> Make your own subclass of JDialog that has such a method, and return a
> value from it that you test. Preferrably against a constant like an
> int, and not against a component. Example:

Thanks a lot!
Cheers