Introduction to Java using NetBeans

Section 2 - GUI


Creating a Project with a GUI

  1. Click on the New Project icon in the toolbar or from the menu bar select File, New Project.
  2. Create a new Java Application
  1. Enter CapturingData in the Project Name field and specify your project location.

4. Right Click on CapturingData in the Projects List. Choose New > JFrame Form and Name your Form




Building your GUI

This is the main window of your capturing input program.
  1. Drag a Label from the Palette to your application window.
    Double click on it and change the text to read, “Enter Your Name:”.
    In Properties window, modify font and foreground
  2. Drag a Text Field from the Palette
    In the Properties window find the text property and remove any text you find there so that your field is blank to start with.
  3. Drag a Button from the Palette to your application window and place it as shown in the image above.
    In the Properties window find the text property and change it to “Enter”.
  4. Drag another Label from the Palette to your application and place it under the button.
    Change the text property to “My first GUI program”.

Modify your form and make it look a little better

  1. Change the component names to something more meaningful: nameLabel, nameInput, enterButton, and outputLabel.
    Right click on the component and choose change variable name:

Adding Functionality


  1. Right click on the Enter button. From the menu that appears select Event, Action, ActionPerformed. This will create what is called an ActionListener for the Enter button. This means that the program will be listening for the ActionPerformed event to occur, and if it does, it will run the set of instructions that you give it within that event. The ActionPerformed event listens for the button to be pressed by either the mouse or the keyboard.
  2. At this point the IDE will display the code that goes along with your application. Netbeans has added lots of code, locate this line

This is the auto created function for the Enter button.

  1. We are now going to add code that controls what the Enter button does.

This is the completed code for the Enter button.

Congratulations. You have now just completed your first program with a GUI. It is now time to make it run



Modify the Capturing Data code as follows:


Modifying the Code

Now that you have a running program with a working GUI, it is time to experiment. Try changing the properties of some of the components. Try adding new components. Play around with the code and see what you can do.

A good place to start is by adding a new button and labeling it Exit. Now add an event to this button that causes the program to close when the button is pressed. Use the following code inside your ActionPerformed method to end the program:




Use the website below to find Java String Methods to complete the programs below.



For all assignments marks will be given for form design and layout


Bonus/ Above and Beyond Marks are in effect for these assignments.

Bonus items must include coding, not just pictures/colours/fancy buttons (these are part of layout and design)



Assignment 2.8 - Birthday Program

Create a Family Birthday Program that makes use of the following:

  • Three labels displaying the title and information requests.
  • Two input text fields for name and date.
  • A label to display the resulting message.
  • A button.


A sample layout for the input/output screen is shown below.

Create your own version of this screen using a different background, colour, and/or a different text font style/size. Make it look amazing.

This shows a possible output screen for the Happy Birthday program.

Note: Use a Panel the size of your form to set the Background!

Add an Image


Modify the Program so that when the user enters First Name and Last Name in the "Enter Name" field the program the following output will be displayed



Jane Doe
Feb. 29, 2015


DOE Family Birthdays

Jane's Birthday is on Feb. 29, 2015


ICS4U modify the program to include 3 additional names. Using the Lastname print Birthdays as shown belot


DOE Family Birthdays

Jane's Birthday is on Feb. 29, 2015
Jack's Birthday is on Mar. 15, 2010

SMITH Family Birthdays

John's Birthday is on Jan. 1, 2011
Jane's Birthday is on June 3, 2007


Add a Reset Button

Assignment 2.9 - Monogram

Create a monogram application that prompts the user to enter his or her first, middle, and last names and then displays a monogram with the first and middle initial in lowercase and the last initial in uppercase. (You already have most of this code done)

Error Checking - If the user enters less/more than 3 names an error message should appear

ICS4U - Give the user the option of adding/changing 1 or more of the following items to the monogram. You will have to use check boxes

ICS4C - Give the user the option of adding/changing one of the options below

- Add a picture
- Change the font Type
- Change the font Size
- Change the font Colour



Assignment 2.10 - Replace String

Create a Replace String application that displays the new string in a lable. The new string should take a sentence entered by the user and replace every occurence of a substring with the new string supplied.

Make sure that your program is case insensitive

i.e. Tomato should also be found when they search for tomato


ICS4U modify the program so that the user can modify all instances or only the first instance. Use radio buttons for the choices

Assignment 2.11 - Remove String

Create a Remove Sting application that displays a new string in a label. The new string should take a sentence entered by the user and remove every occurrence of a substring supplied by the user.


Modify the program so that the user can remove all instances or only the first instance. Use radio buttons for the choices.

ICS4U add an insert string option .



Posting your Work

Complete the S2 Rubric and place it in a Rubrics folder on your Google Drive. Make sure I have access to edit this file.

Put a link to your Rubric on your website.

Include you drive folders with link to folder, link to jar file, code.

Creating a Jar File
Right-click on the Project name
Select Properties
Click Packaging
Check Build JAR after Compiling
Check Compress JAR File
Click OK to accept changes
Right-click on a Project name
Select Build or Clean and Build