3 minute read

In this workout, you are asked to build a form for a Java class Triangle by using Java Swing library.

Step 1: What is the expected outcome?

Download Triangle.jar. Double click the file to launch the form.


The form supports the following operations:

  1. Data entry: The user will enter an integer into each of the three input fields.

  2. Data processing: The user can click the Validate Triangle button to see whether the three sides will form a triangle. The check result should be displayed in the output text area in the middle of the form.

  3. The supplementary feature: If the user clicks the Clear button, all the current text in the input fields and output text area will be removed.

Step 2: Download the NetBeans Project

A file structure has been given in a NetBeans project.

Download the project TriangleTest.zip

Extract the zip file. It should contain the folder Triangle which is a NetBeans project.

Step 3: View the project files

Launch NetBeans IDE.

Open the project Triangle.

Under the Project tab, you should see a tree view of the packages gui and mytools and one class Triangle.java.


Step 4: Create a New JFrame Form

In the package gui, create a new JFrame Form.

Name the form Detector.


After step 4, the file structure in the project should be like


Step 5: Add UI Components

Now you will add all the components to the form.

The required components are shown in the following Navigator view and layout view.



Step 6: Add a JLable

Add an additional component into the form: A JLabel showing your name.

Step 7: Bind an Event with the Clear Button

If the user clicks the Clear button, remove the current text in both input fields and output text area.

Step 8: Bind an Event with the Validate Triangle Button

If the user clicks the Validate Triangle button, the actions to be performed are listed below:

s1. Read text input from three input fields.

javax.swing.text.JTextComponent.getText() will read the text from the caller into a String.

s2. Extract an Integer from the raw text input.

As the content in text-based components are read as string, parse the integer value from a String, apply Integer.parseInt to the string.

For example, to extract an integer from the String rawinput, the expression is


s3. Call Triangle.isValid method to find out whether the three sides will form a triangle.

s4. Display the check result in the output text area which is located in the middle of the form.

javax.swing.text.JTextComponent.setText(r) will write a string r to the caller.

Step 9: Test the Form

Step 10: Submission

Compress the entire NetBeans project in a zip file.

Submit the zip file.

comments powered by Disqus