Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

JTextFields (part of GUI) in Java

Status
Not open for further replies.

Sputnik

Full Member level 3
Full Member level 3
Joined
Oct 19, 2004
Messages
150
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,298
Location
South Africa
Activity points
1,667
I have a project where I am required to write a GUI program. My problem being is that I need to have an integer and double read from two different text fields. I have not learnt enough about GUI and Java and I am stuck as I only know how to extract a string from a text field not numerical values that can be used later for computation. We have to use the ActionListener class (import java.awt.event.*). I'm using "Ready to Program IDE".

Thanks
Sputnik
 

For example to convert a JTextField entry into an integer you could do this:

Code:
JTextField field = new JTextField();
...
...
int number = Integer.parseInt(field.getText());

For your program you will need to do both an integer and a double, perhaps in an if-else type construct if you need each textfield to handle both number types, otherwise if one handles only integers and the other handles only doubles then you're all set.

Integer has parseInt(string) and Double has parseDouble(string). Have a look at: https://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html
and https://java.sun.com/j2se/1.5.0/docs/api/java/lang/Double.html

Also make sure you do the parsing within a try catch block for NumberFormatException as this will catch entry errors.

For the .getText() you might need to try catch for NullPointerException to make sure there is text available.



If you're using SDK 1.4 or greater try: JFormattedTextField have a look at it here https://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JFormattedTextField.html, for example:
Code:
JFormattedTextField field = new JFormattedTextField(new Integer());
This force the text field to be an integer always, have a look at the link above to get more ideas. The nice thing about this is that say a float like 3.141592 can appear as 3,141592 depending on locale setting of the application.

I hope this helps.

- Jayson[/code]
 

    Sputnik

    Points: 2
    Helpful Answer Positive Rating
Thank you, very helpful but I do not know how to do a "try catch block for NumberFormatException." As well as the "NullPointerException" for .getText() I am still very new to Java.

Thanks
Sputnik
 

In Java, you use try catch for exceptions. For instance lets say you hardcoded in your program to open a text file, but that text file is not available? Now what? This is why we need try catch.

Most user input methods come with exception handlers. Have a look at this link: https://java.sun.com/docs/books/tutorial/essential/exceptions/handling.html

Read the API documentation links I posted in my first post, every method described will state exceptions, if any.

- Jayson
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top