aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTextField with JOptionPane - showMessageDialog (please help) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField with JOptionPane - showMessageDialog (please help)" Watch "JTextField with JOptionPane - showMessageDialog (please help)" New topic
Author

JTextField with JOptionPane - showMessageDialog (please help)

Ross Einmart
Greenhorn

Joined: Feb 10, 2011
Posts: 3
Here's the deal.This is my school assignment.To make calculator using JRadioButtons and JButton and JOptionPane - showMessageDialog (in short).
I did it all, and i miss only one thing, that is:
When i enter numbers in two JTextFields, in case that this entered number is decimal(float, double) i should get showMessageDialog that says "You can only enter whole numbers"
That's the whole deal.How do i do that?
Thanks
By the way, yes i am new to Java

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Welcome to the Ranch, Ross!

Could you please UseCodeTags next time? I've added them for you this time.

What happens currently when you input a wrong number? Does something show up in the console? (hint hint)


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ross Einmart
Greenhorn

Joined: Feb 10, 2011
Posts: 3
Thanks for UseCodeTags, i'll remember.
Nothing happens when i input a wrong number, it just won't calculate with decimals (as I intended).
And as I said that's fine I just need showMessageDialog.
Amolk Patil
Greenhorn

Joined: Feb 08, 2011
Posts: 5
HI

I thing you have to validate input string. Refer the modified code below.

Hope this will help you.

Good Luck


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Ross Einmart wrote:Nothing happens when i input a wrong number, it just won't calculate with decimals (as I intended).
And as I said that's fine I just need showMessageDialog.

Trust me, something happens. Not in the user interface, but check the console. You can use that "event" to catch (hint hint) users entering incorrect numbers.

Amolk Patil wrote:

Nice try, but there are some problems here.
1) strValidate.equals(null) always returns false, so either the body is executed or a NullPointerException is thrown. Use == and != to compare to null.
2) don't use toCharArray() to iterate over the contents. Use length() and charAt(int).
3) don't use an empty body for the if statement, just to be able to execute the else statement. Negate the guard instead.

4) what will you do if I enter 9876543210? That's valid according to your method, but it's too large for ints.
Amolk Patil
Greenhorn

Joined: Feb 08, 2011
Posts: 5
Thanks Rob Spoor for your valuable suggestions in my code.

I think we have to handle NumberFormatException exception here.

Something like this..

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Catching that exception was what I was hinting at, yes. But you don't need your own validation anymore. The parsing to int and catching the exception will do that for you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39380
    
  28
You don't usually need to say getContentPane().add(). You can simply write add().
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

True, but only since Java 5.0. In Java 1.4 and before, calling methods like setLayout, add and remove directly on frames / dialogs would throw an Error. Not an exception, no, a real genuine Error:
A lot of people still are using getContentPane() because of that.
Ross Einmart
Greenhorn

Joined: Feb 10, 2011
Posts: 3
Thanks a lot guys.
This really solved my problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextField with JOptionPane - showMessageDialog (please help)