This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes JOptionPane dialog box is not displaying Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JOptionPane dialog box is not displaying" Watch "JOptionPane dialog box is not displaying" New topic
Author

JOptionPane dialog box is not displaying

kunagu varun
Ranch Hand

Joined: Apr 13, 2010
Posts: 38
Hello friends i am new to java programming. I am trying to develop a simple GUI based application to perform addition of two numbers.

Please have a look at my code


What i am expecting is whenever a user presses on the add button and if the input numbers are not provided then a message box much appear and display the error message.
The above code works fine and the box is also displaying but I need it to appear only after checking that if condition. If I place the JoptionPane line inside that if (Yes I have removed the block comment) nothing appears.

Hope you have understood my problem. Please help me guys
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

That is because you are using == to compare strings. The == operator compares references - in other words, it checks if the two arguments to the left and right of it refer to the exact same object. It does not compare the content of the objects.

Use equals instead:



Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Did you check what value is returned from txtNum1.getText() and txtNum2.getText()?

Maybe it is not having an empty String ("") and thats why the comparison is failing?

Cheers,
Raj.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Jesper de Jong wrote:
Use equals instead:

Use equals as said by Jong... or check whether text is of zero length

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Raj Kamal wrote:Maybe it is not having an empty String ("") and thats why the comparison is failing?

I fear even if it returns "" it will fail if we use == for comparison
kunagu varun
Ranch Hand

Joined: Apr 13, 2010
Posts: 38
Thank you Mr.Jesper de Jong
Worked like a charm
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Gosh! Can't believe I missed the '=='...



 
Don't get me started about those stupid light bulbs.
 
subject: JOptionPane dialog box is not displaying