9

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.

The main problem I have is that my code executes regardless of me pressing cancel and a socket connection does get made even if I don't add any input.

Why does this happen and how can I avoid this?

String input = "";
           try
           {
               InetAddress host = InetAddress.getLocalHost();
               String hostAddress = host.getHostAddress();

               //setting label to host number so as to know what number to use
               labHostName.setText("(" + hostAddress + ")");

               input = JOptionPane.showInputDialog(null,"Please enter host name to access server(dotted number only)...see number on frame", "name", JOptionPane.INFORMATION_MESSAGE); 

               if(input != null && "".equals(input))//input != null && input.equals(""))   
               {
                   throw new EmptyFieldsException();



               }
               else if(input != null && !input.equals(hostAddress))
               {
                   throw new HostAddressException();


               }

               else
               {

                    clientSocket = new Socket(input, 7777);

So with the code being the way it is at the moment the clientsocket connection is made even if I do press cancel. Is the reason for this perhaps because I have the Server and Client as two seperate programs on the same machine? How can I avoid this from happening?

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
Arianule
  • 8,811
  • 45
  • 116
  • 174
  • For better help sooner, post an [SSCCE](http://sscce.org/). BTW - `showInputDialogue` & `inputDialoguebox` no such things. Please take more care typing posts. – Andrew Thompson Mar 16 '12 at 08:02

3 Answers3

9

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();
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
2

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!

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Ahmed Ktob
  • 2,874
  • 1
  • 11
  • 15
1

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.

Rahul Borkar
  • 2,742
  • 3
  • 24
  • 38
  • 3
    Seems like you doing a bit of mistake, the value returned by `showInputDialog(...)` is a `String` and you comparing it with an `int` value of `JOptionPane.CANCEL_OPTION`. Am I wrong ? – nIcE cOw Mar 16 '12 at 08:17
  • Yes I have just stated a way of doing that and what questioner is missing. – Rahul Borkar Mar 16 '12 at 09:38