Clicking the cancel button showInputDialogue

前端 未结 3 1470
花落未央
花落未央 2021-01-12 07:29

I have a question in regards to pressing the cancel button of my inputDialoguebox. I have asked a similar question before so I apologize if I seem to repeat myself.

<
相关标签:
3条回答
  • 2021-01-12 08:00

    It will always go in else condition even if cancel button is pressed. Check for,

    else if(input == JOptionPane.CANCEL_OPTION){
       System.out.println("Cancel is pressed");
    }
    

    add above code before last else statement explicitly, and handle cancel button pressed there.

    0 讨论(0)
  • 2021-01-12 08:00

    I had this same issue, and I solved it as follow:

    if(input != null){
        if(!input.isEmpty()){
         // Do whatever...
        }
    }
    

    So, I basically moved the null test before testing if the user has entered some input. Hope this helped!

    0 讨论(0)
  • 2021-01-12 08:23

    When you click on the Cancel Button of the showInputDialog(...) , you always get a null value, for which no condition is satisfied, hence a new connection is always established. So you can add this condition like this :

    if(input == null || (input != null && ("".equals(input))))   
    {
        throw new EmptyFieldsException();
    }
    
    0 讨论(0)
提交回复
热议问题