6

I have a custom dialog box that collects two strings from the user. I use OK_CANCEL_OPTION for the option type when creating the dialog. Evertyhings works except when a user clicks cancel or closes the dialog it has the same effect has clicking the OK button.

How can i handle the cancel and close events?

Heres the code I'm talking about:

JTextField topicTitle = new JTextField();
JTextField topicDesc = new JTextField();
Object[] message = {"Title: ", topicTitle, "Description: ", topicDesc};

JOptionPane pane = new JOptionPane(message,  JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
JDialog getTopicDialog =  pane.createDialog(null, "New Topic");
getTopicDialog.setVisible(true);

// Do something here when OK is pressed but just dispose when cancel is pressed.
mKorbel
  • 109,525
  • 20
  • 134
  • 319
banyard
  • 149
  • 2
  • 2
  • 9

3 Answers3

5

I think a better option for you would be to use the following code

    JTextField topicTitle = new JTextField();
    JTextField topicDesc = new JTextField();
    Object[] message = {"Title: ", topicTitle, "Description: ", topicDesc};


    Object[] options = { "Yes", "No" };
    int n = JOptionPane.showOptionDialog(new JFrame(),
            message, "",
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
            options, options[1]);
    if(n == JOptionPane.OK_OPTION){ // Afirmative
        //.... 
    }
    if(n == JOptionPane.NO_OPTION){ // negative
        //....
    }
    if(n == JOptionPane.CLOSED_OPTION){ // closed the dialog
        //....
    }

by using the showOptionDialog method, you are getting an result based on what the user does, so you don't need to do anything else except for interpret that result

PTBG
  • 585
  • 2
  • 20
  • 46
2

JOptionPane returns in your case

JOptionPane.OK_OPTION
JOptionPane.CLOSED_OPTION
JOptionPane.CANCEL_OPTION

simple example here

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

See Class JOptionPane. Start reading in the text at point "Examples:"

Here is my complete example:

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Main {
public static void main(String[] args) {

    JTextField topicTitle = new JTextField();
    JTextField topicDesc = new JTextField();


    Object[] message = {"Title: ", topicTitle, "Description: ", topicDesc};

    JOptionPane pane = new JOptionPane(message,  
            JOptionPane.PLAIN_MESSAGE, 
            JOptionPane.YES_NO_CANCEL_OPTION);

    JDialog getTopicDialog =  pane.createDialog(null, "New Topic");
    getTopicDialog.setVisible(true);        

    Object selectedValue = pane.getValue();
    int n = -1;


    if(selectedValue == null)
        n = JOptionPane.CLOSED_OPTION;      
    else
        n = Integer.parseInt(selectedValue.toString());


    if (n == JOptionPane.YES_OPTION){
        System.out.println("Yes");
    } else if (n == JOptionPane.NO_OPTION){
        System.out.println("No");
    } else if (n == JOptionPane.CANCEL_OPTION){
        System.out.println("Cancel");
    } else if (n == JOptionPane.CLOSED_OPTION){
        System.out.println("Close");
    }       
}
}
René
  • 194
  • 1
  • 8