Вопрос

i use the following Code to get an InputDialog:

String c = JOptionPane.showInputDialog("Select number",JOptionPane.OK_OPTION);

I also want that the User only uses IntegerValues between 0 and 100. I handle this by the following code:

while(notAllowed){
    try{
        int t =Integer.parseInt(c);
        if(t==JOptionPane.CANCEL_OPTION)
        {
            notAllowed=false;
            cancel=true;
        }
        if(t<=100 && t>0 &&notAllowed)
            notAllowed=false;
    }
    catch( Exception err)
    {}
    if(notAllowed)
        c = JOptionPane.showInputDialog("Only Numbers between 1 and 100 are allowed");
    }

Now if the uses types the number 2 its like clicking the Cancel Button because the Value of JOptionPane.CANCEL_OPTION is also 2. So how can i find out if Cancel is clicked or the input Value is 2.

Это было полезно?

Решение

JOptionPane returns null if the user clicks on cancel. Otherwise the value will be returned. I figured that out using this small example:

public class JOptionPaneTest {

    public static void main(String[] args) {
        Object obj = JOptionPane.showInputDialog(null, "test", "test-text");
        System.out.println(obj);
    }

}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top