Important Note: If you haven’t read Part I or learned the fundamental concepts of Java Swing yet, I’m encouraging you to read that part. It contains preliminary concepts about Java Swing, and these are essential to have an understanding about desktop application development. Tap or click this link to proceed to Part I.
Has learned the Part I of Java Swing
These following components can be found at the Palette’s Swing Controls:
Text Field – It is a Swing component used to provide an area for entering text input.
Label – A component for displaying text.
Button – A minimal clickable component for executing particular actions.
To change the display text of any component, select the component (click the component), then right click, then click Edit Text, after editing press Enter on your keyboard.
For this document, the layout for the underlying application should look like this. To add text within the application, use Label and edit its text to your desire. To add an input field, use Text Field and also edit its default value (text) to blank.
Renaming variables is important for managing multiple components that their variables might be used multiple times. Most variables of the newly created components have autogenerated names, which are generated using the name of the component and a number that increments if it detects an existing component. To rename a component, right-click on the component, then click Change Variable Name.
This dialog will pop up; you can rename your variable with any name you want, as long as it is a valid variable name, so no white spaces, no number at the beginning, and no special characters. After providing a name, press OK to rename the variable.
Now we have two input fields, we have to perform an arithmetic operation which is calculating the sum of two numbers. You only need to modify the Calculate button’s function to perform the sum calculation of two numbers, which are provided from the values of those text fields.
The entire code for performing addition looks like this:
It has only 4 lines of code, let’s go through these lines of code. Obtaining text from text fields returns a String; it's not eligible for performing arithmetic operations, and it needs to be converted to an integer to make it an actual number that can be used for such operations like this.
To convert a string into an integer, use the Integer.parseInt() method. And assign each parsed string to a variable.
After parsing, create a variable to store the sum of two numbers (strings that are converted into numbers) added.
Then finally, add a line to show the sum using the message dialog once values are provided by the user.
Run the application once finished, enter any number on these two input fields (in this instance, both 23), then press Calculate (or whatever you named for this button) to show the sum of two numbers and must meet the expected result (46 in this instance).
NOTE: An exception occurs if the provided values from input fields aren’t numbers, e.g., “3t”, “y5”, “hello”, etc. So, only enter numbers to get it work, rather than overthinking about why that error occurs.