Solved

JTextField Exception

Posted on 2004-10-18
4
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Restore security prompts not working 10 236
Java program running SQL query 5 59
spring maven example issues 3 93
maximize the sum of fractions 33 70
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

738 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