This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread 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 Android Security Essentials Live Lessons this week in the Android 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: 14074
    
  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 '=='...



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JOptionPane dialog box is not displaying
 
Similar Threads
How to display sum of numbers from a java class to a jsp using custom tag?
Mission accomplished.. thanks everyone...
Erroneous sym type What does this mean?
HELP!!!! how does Character.isLetter work? (need experts)
[HOMEWORK] Please need help with java project