wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JTextField  InputVerifier 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  InputVerifier" Watch "JTextField  InputVerifier" New topic
Author

JTextField InputVerifier

Garandi Garandi
Ranch Hand

Joined: Jan 07, 2003
Posts: 192
Dear Reader,
I have a class which extends JPanel, which in turns has a JTextField. I want to verify the input to JTextField using

In inputVerfier(comp) I have a JOptionPane and when JOptionPane gets executed I get a nullpointer exception and the program crushes and even JBuilder stop responding.(although I am not running the code from JBuilder)
Code for inputVerfier(String content)

I am wondering what is going wrong.
Thank you
Garandi
[ June 13, 2003: Message edited by: Garandi Garandi ]
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

USE this code instead and let us know if you still have a problem.

HTH,
- Manish
Garandi Garandi
Ranch Hand

Joined: Jan 07, 2003
Posts: 192
Your code is same is mine and this would NOT work either.
Garandi
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Manish
I think that the follwing code still have problem

//Because without the variable to store the return value of the (1) so (2) may still contain problem.
(1)Double.parseDouble(content);
(2)Double value = new Double(content);
Use the following code to substitution.
Double value = new Double(Double.parseDouble(content));
If the coding still have compiler error or logic error,please post it again


Francis Siu
SCJP, MCDBA
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Why not just:
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
Chris
You said the truth,but the following statement is within try block,so if only using Double value = new Double(content);
I concern that it does not throw exception
But after reading the following statement, it throw exception
if(value.doubleValue() <= 0)
So you can consider WHEN the exception throw,of course, it does not matter.
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Originally posted by siu chung man:
Chris
You said the truth,but the following statement is within try block,so if only using Double value = new Double(content);
I concern that it does not throw exception

It certainly will throw an exception if the String cannot be parsed. Read the javadocs for Double.
[ June 15, 2003: Message edited by: Chris Mathews ]
Garandi Garandi
Ranch Hand

Joined: Jan 07, 2003
Posts: 192
The problem is with

which is called inside the method and the method is called inside a inner class.
Thank you
Garandi
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
It certainly will throw an exception if the String cannot be parsed.
Yes,I know,but it can not throw NumberFormatException
You may be missunderstanding what I want to express.
Do the input must be String so that it must convert successfully?
Except for the Null Pointer Exception
Garandi Garandi
Ranch Hand

Joined: Jan 07, 2003
Posts: 192
If the input is not a number I want it to throw an exception.
But the problem is with JOptionPane line in the code. I don't know why?
Thank you
Garandi
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Originally posted by siu chung man:
Yes,I know,but it can not throw NumberFormatException
You may be missunderstanding what I want to express.
Do the input must be String so that it must convert successfully?
Except for the Null Pointer Exception

Why can't it? In fact it certainly does throw a NumberFormatException. See the javadocs, they clearly state that the Double(String s) constructor will throw a NumberFormatException if the String cannot be parsed.
Sorry for the hijack. :roll:
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
Chris
yes,I read it.
But the problem is with JOptionPane line in the code. I don't know why?
I think that (this) can not refer to the top level window,so you should get the top level window object to that to show it out.(because you are using the JPanel in this class)
Please try it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextField InputVerifier
 
Similar Threads
How to track backspace key?
InputVerifier
inputverifier - work with JOptionPane?
Is there a standard way of creating a text field that requires the input to be an integer?
Disappointed with JFormattedTextField