|
RHS |
Read Input 02
In ReadInput01 you made a simple GUI where text from a text box by the click of a button was "moved" to a label.
You also learned that
If you want to learn more details about these methods or other methods available in the GUI components you've used so far you should consult the Java API documentation (your best friend!).
Now we'll extend the application a little bit - and learn how to copy files from one NetBeans project to another!
Create the project
As usual you have to make some decisions:
| Location of project files | <your NetBeans projects folder> | ||
| Name of project | ReadInput02 | ||
| Name of class(es) | InputForm02 (a JFrame class) | ||
| Create a class with main() method? | No - not necessary as we're working with only one simple GUI-class |
So open NetBeans and create a new project. REMEMBER to uncheck the "Create Main Class" choice.
Copy a file
Instead of creating a new JFrame class from scratch we'll copy the one we made inReadInput01.
So open the project InputForms (the one you created inReadInput01), go to the<default package>, right-click on the InputForm01.java and choose "Copy":
![]() |
![]() |
||
| In <default package> you find InputForm01.java | ... right-click and choose "Copy" |
Now go to the<default package>in the new ReadInput02 project, right-click and choose "Paste".
![]() |
![]() |
||
| ReadInput02's <default package> | ... right-click and choose "Paste" |
Close the InputForms project.
Rename the file
But wait! The name of the copy is wrong - and remember that class-names, file-names and so on must match?
How do we make sure that we rename all needed information in one go?
Right-click on the class, choose "Refactor" and then "Rename" (next time you simply press <Alt><Shift><r>).
In the dialog box change the "New Name" to "InputForm02", check the "Apply Rename on Comments" and click "Next".If the "Preview All Changes" was checked you have to click the "Do Refactoring" button in the "Output" section where you have a chance to alter the action - if it was not checked the renaming will be done without further notice!
![]() |
If "Preview All Changes" is checked you get a chance to select excatly what will be renamed or not. |
Extend the GUI
Now open the InputForm02 file and change the buttons text, add two JLabels and a JTextField.
Rename the two text fields to: n1Fld and n2Fld.
![]() |
![]() |
Click on the "Source" button and find the actionBtnActionPerformed() method (the one you made in ReadInput01) - now we're going to change the method so it executes the functionality we need in this application.
![]() |
![]() |
||
| The "Source" button is next to the "Design" button. | The actionBtnActionPerformed() method |
Convert Strings to int
We want to read the values from the two text fields, add the two numbers and put the result on the output-label.
BUT - the method getText() from the JTextField returns a String - we need an int number!
Luckily for us the class Integer has a method called parseInt() that takes a String as parameter and returns the matching integer number as an int.
The statement int n1 = Integer.parseInt( n1Fld.getText() );
will read the String from the n1Fld text field, use it as parameter to the parseInt() from the Integer class and finally assign the resulting integer value to an int variable called n1.
Finish the method
Read the content of the other text field (n2Fld), convert it to an int and store it in a variable (n2).
Finally set the text of the outputLbl to be the result of the addition of the two numbers.