Solved

JTextField Exception

Posted on 2004-10-18
4
383 Views
Last Modified: 2008-03-17
I have a JTextField and an enter button. The user will enter a number into the JTextfield and hit enter when he is done.

What is the best way to read and write to a JTextField box. I want to limit the input field to numbers
between 1 -100. The problem I have is if the field is empty whent the enter button is pushed I receive an exception.

Here is my code:
        Object oHOTValue;
        try
        {
            oHOTValue = heightOfTargetTextField.getText();
        }
        catch (NumberFormatException s )
        {
            oHOTValue = "1";
        }
        String sHOTValue = oHOTValue.toString();
        int iHOTValue = Integer.parseInt(sHOTValue);
        zoomConfig.setTargetHeight( iHOTValue );
        iHOTValue = zoomConfig.getTargetHeight();
        String sValue = Integer.toString(iHOTValue);
        heightOfTargetTextField.setText(sValue);
        System.out.println("HOT button pressed : " + iHOTValue);

The try catch is not working correctly.

thanks

0
Comment
Question by:hugoitt
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 125 total points
ID: 12341186
Hi hugoitt,

The try-catch clause is in the wrong location!

        String sHOTValue = heightOfTargetTextField.getText();
        int iHOTValue = 1;
        try
        {
            iHOTValue = Integer.parseInt(sHOTValue);
        }
        catch (NumberFormatException s )
        {
            // already has default value
        }
        zoomConfig.setTargetHeight( iHOTValue );
        iHOTValue = zoomConfig.getTargetHeight();
        String sValue = Integer.toString(iHOTValue);
        heightOfTargetTextField.setText(sValue);
        System.out.println("HOT button pressed : " + iHOTValue);

\t
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12341194
The try-catch is working correctly.
Why don't you:

1)  String input = textfield.getText();
2)  int value;
     try
     {
           value = Integer.ParseInt(input);
     }
     catch(NumberFormatException e)
     {
..... your action on bad input ....
     }

;JOOP!
0
 

Author Comment

by:hugoitt
ID: 12341474
thanks grangehead911
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12341626
=-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 86
DO we need Java installed on a Windows PC and WHY ? 13 98
Strange loading of website behaviour 3 32
stackato and cloud 4 73
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now