Java Swing Basics with Netbeans
Tutorial content,
- Creating a new java project
- JFrameForm
- JPannel
- JLabel
- JTextField
- Adding and writing code in a JButton...
1. I
hope you have already installed JDK and Netbeans in your machine, so let us
begin by creating a new java project ... In Netbeans go to File à New Project à in the categories section select
Java, then in the projects area,
select Java Application. Click Next.
2. This
page allows you to enter the project details, let’s change the project name
first. In the Project Name text box
enter the new project name, I will name it as SwingTute1. Also pay attention to the Project Location and Project
Folder text boxes, there you will find the location of the Netbeans
project. Now, un-check Create Main Class.
Click Finish.
3. Now
you will see an empty project. If you expand that project, you will see a
folder called Source Packages. Right
click it and go to New à Other à in the categories section select Java and in the File Types section select Java
Package. Now click Next.
4. In the
new page, enter swingUI for the, Package Name, remember as a convention,
we should enter package names staring with a simple letter. (This simple and capital
letter naming convention, which will be used in this tutorial is called the camel
notation, that is, the beginning of the name starts with a simple letter, all
the other new words start with a capital letter, as an example ...
thisIsCamelNotation :D ). Click Finish.
5. Now
you will be able to see a newly created package called swingUI in the source
packages folder ...
6. Right
click on the newly create swingUI
package and go to New à Other à
in this window, under the categories select Swing GUI Forms, and for the file
type select JFrame Form. Then press Next.
7. In the
new window, for the class name, enter NamePrinter, we start a class name with
the capital letter. Make sure package is swingUI. Press Finish.
8. Now
you can see the newly created JFrameForm inside the package ...

9. Now
let’s add some elements to the JFrame form... but first let’s learn some
interesting facts about JFrame forms. Remember, if you need to execute a file,
you should always have a main method for that file, but we can run JFrameforms,
why is that? Simple, it’s because JFrameform has a main method. The Netbeans
auto generates that code. If you want to see the main method of the JFrameForm,
click on the Source button and check out the code for the JFrameForm.Now let’s add some elements to
the JFrameForm. First add a JPannel on to the JFrameForm and size it as necessary.Add 2 text boxes(jTextFields). Right click on
each jTextField and select change variable name. Enter jTextFieldFirstName for
one variable name and for the other put jTextFieldSurname.Add a jLabel to the panel and
change its variable name to jLabelDisplay.Add a jButton to the jPannel
and change its variable name to jButtonDisplayValues and change its display
text to Click me.
Notice that we are using
naming conventions, we use this because, if it will be easier to find those
variables with the inteli-sence of Netbeans IDE. As an example, if you need to
see all the text boxes in a particular JFrameForm, go to source and type in
some part of the variable name ( ex. jTextFi ) and press Ctrl+Space, they you
will be able to see all the variables and methods starting from the letters “jTextFi”
So,
back to the frame form... the final form should look like this...
(oh and by the way get rid of
those default names displayed in the jTextFields and jLabel) you can do it by
right clicking a particular element in the inspector window and selecting Edit
text.
(Notice you cant see the label once you remove the default text from it, make sure is its expanded enough to display text later)
10.
Now
let’s add some code to the button (jButton). To add code, double click on the button. Then
it will display the code behind, in this case the Netbeans will take you to the
button click method. When you click that particular button, the code inside
that method will start to execute ...
Initial Button Click method ...
private void jButtonDisplayValuesActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
Enter the following code IN SIDE the button click method.
String fullName =
jTextFieldFirstName.getText()+" "+jTextFieldSurName.getText();
jLabelDisplay.setText(fullName);
Here, the getText method of the jTextFieldFirstName is
called (which retrieves the entered text from that text field) and + concatenates
the taken text with a “ “ (space) then the text from the other text field is
retrieved and the full String is assigned to the String variable fullName.
In the next line,
setText method of the jLabelDisplay object is called and the String in
the fullName variable is passed to that method as a parameter.
so in the end, complete button click method should look like this,
private void jButtonDisplayValuesActionPerformed(java.awt.event.ActionEvent evt) {
String fullName = jTextFieldFirstName.getText()+" "+jTextFieldSurName.getText();
jLabelDisplay.setText(fullName);
}
11. Now
go to the source packages à
swingUI à
NamePrinter.java, right click it and select run.
12. Enter some values and press the button and you will see the out put values in the label
So, there you have it. A simple swing application, with user data input, out put and a button click event. Remember that, building of this small application will be the basis of other tutorials. When I was learning Java swing for the first time, I created a simple application like this.
I also played around with the properties of each element in the JFrameForm, therefore I hope you do the same.
Right click on an element on the JFrameForm and click properties, then change stuff and see what happens.
If you have any problems regarding this tutorial please post them in the comments section and I will be more than happy to reply you.
Thank you.