Adding a JFrame

When you click Finish an initial default project is created for you. You can see its structure in the Projects window:

The only folder that matters at this stage is the Source Packages folder. This is where all of the files containing Java that you create will be stored.

In this instance we need to create a file to start writing some Java. Use the:

File,New File

command.

The dialog box that appears gives you a choice of a range of different types of file you can create. In this case we need to create a basic Swing file so select Swing GUI forms and then select JFrame Form.

A JFrame is the basic display surface that you can place buttons, textboxes and so on to create a user interface so it is often the starting point for an application. When you click the next button you are given an opportunity to give the class a name and customise it.

For the moment simply accept the defaults and click Finished.

The new JFrame file is created for you and placed in the project within the default package - more about packages later.

You will also see the designer showing you a blank grey area which represents the JFrame. You can also see two tabs - Source and Design.

If you click the Source tab you can see the code that has been generated to create the JFrame.This code will make sense to you in a very short time working with it but for now ignore it.

Adding a button

If you click Design you can use the Palette (top right) to place user interface object on the JFrame. Scroll down in the Pallet until you find a Button -- under the Swing Controls section -- and drag-and-drop the button onto the surface of the JFrame in the designer.

You can now position and size the button by dragging it to where you want it or dragging the sizing handles.

To make this our first program in Java we really do have to enter some Java instructions. Double click on the button and you will automatically generate a click event handler - i.e. a block of Java code that is obeyed in response to clicking the button. The default handler that NetBeans generates for you doesn't do anything at all:

As you can see there is a comment line inviting you to add your own code that will be run when the button is clicked.

If you want to see what your user interface looks like when the program is run you can click the Preview icon.

Adding some code

To make something happen when the button is clicked we can make use of the JOptionPane object which has a range of methods that can be used to make message boxes pop up. Before we can use JOptionPane we have to tell the compiler that we are intending to use it and you need to add:

import javax.swing.JOptionPane;

to the start of the file.

With the import statement in place we can now write a line of code that makes a message box pop up. Edit the click event handler to read

Don't worry too much about the actual instruction but if you read it then it should make reasonable sense. The "Hello Swing World" appears as the message and "Hello" as the window title. The JOptionPane.INFOMATION_MESSAGE simply sets the type of pop up box. You may also notice that as you type NetBeans attempts to help you by showing you possible completions of what you are typing, by adding quotes automatically and generally trying to second guess and provide information. The easiest way to find out how all this works is to simply try it out.

If you want to run the program as it is then click the Run icon.

The first time you run the program you will be asked to set the "Main Class" this is just the where the project starts executing code in this case set it to NewJFrame our JFrame class. You only have to do this once but you can change a project's main class anytime you need to.

When you run the project you should see the JFrame complete with button and when you click the button the Hello Swing World message box should pop up.

Now that you have created and run your first Swing Java application it is time to move on and learn some Java.