• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JOptionPane dialog box is not displaying

 
kunagu varun
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Use equals instead:

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

 
John Jai
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mr.Jesper de Jong
Worked like a charm
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gosh! Can't believe I missed the '=='...



 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic