LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   reading JOptionPane when cancel is hit (https://www.linuxquestions.org/questions/programming-9/reading-joptionpane-when-cancel-is-hit-425951/)

Fredstar 03-18-2006 04:31 AM

reading JOptionPane when cancel is hit
 
Sorry for asking a question i could probably find in the api, but i gotta get some sleep before class tomorrow!!

Im trying find out how i could find out if the cancel button is hit in JOptionPane. does it give a null?

like if i was to do the following

String someText =JOptionPane.showInputDialog(null, "enter some stuff");

and the cancel button was hit how would i know?

thanks

Mega Man X 03-18-2006 07:25 AM

Yeah, it will return null if you click cancel. If you click OK without typing anything, then you will get an empty String, which is not null. Test this:

Code:

import javax.swing.*;

public class MainClass {

    public static void main(String[] args) {

        String someText = JOptionPane.showInputDialog(null, "Enter some stuff");
        System.out.println(someText);

        if (someText != null) {
            System.out.println("You've entered: " + someText);
        } else {
            System.out.println("someText was null");
        }

    }
}


Fredstar 03-18-2006 07:31 AM

Quote:

Originally Posted by Mega Man X
Yeah, it will return null if you click cancel. If you click OK without typing anything, then you will get an empty String, which is not null. Test this:

Code:

import javax.swing.*;

public class MainClass {

    public static void main(String[] args) {

        String someText = JOptionPane.showInputDialog(null, "Enter some stuff");
        System.out.println(someText);

        if (someText != null) {
            System.out.println("You've entered: " + someText);
        } else {
            System.out.println("someText was null");
        }

    }
}


Thing is that

String someText = JOptionPane.showInputDialog(null, "hit cancel");

!=null

or better put

String st1, st2;

st1 = new String("null"); //which is how JOptionPane will store it
st2 = new String(null);

st1 != st2

which is what im trying to get arround.


All times are GMT -5. The time now is 01:49 AM.