Creating a Java GUI program with Netbeans


Error message

Deprecated function: The each() function is deprecated. This message will be suppressed on further calls in menu_set_active_trail() (line 2405 of /home/vusanico/public_html/includes/

Creating a Java GUI program with Netbeans

After creating our first program and choosing the right IDE we are now going to create a Java GUI program. In our first tutorial we created a Java program that runs in the console, that means it doesn't have a Graphical User Interface and so you'd have to type out commands in order to use the and navigate the program, if it was a bigger program this could quickly become complex.

A graphical user interface (GUI) is a human-computer interface (i.e., a way for humans to interact with computers) that uses windows, icons and menus and which can be manipulated by a mouse (and often to a limited extent by a keyboard as well)

To start this tutorial we are going to jump straight into our IDE, for this tutorial we are using NetBeans 8 with Java JDK 8_025 click here to download the IDE, you can download only the "Java SE" bundle or download the "All" bundle to have the complete IDE. Once Netbeans is downloaded and installed on your machine open it and create a new Java project.

Create new Java Application

To create a new Java Application click on the new project button "CTR+SHIFT+N" ;-) select the "Java" folder, select "Java Application" and then click "Next".

Choose name and location

On this screen, you give your Java application a name and where it is to be located on your hard drive, unselect "Create Main Class" and click on finish.  Netbeans will generate all necessary files for you and the configurations ;-) so you don't have to do it yourself. We should now be presented by an empty Java project, to create our GUI we are going to use Netbeans GUI builder.

Create JFrame

Right click on the empty package "<default package>" it should be greyed out, select New and then JFrame Form and New JFrame Form will appear, you should now enter a package name, a package name can be anything you choose to uniquely identify that section of your code, once that is done click on the "Finish" button.

A JFrame in Java is a frame, implemented as an instance of the JFrame class, it features a window that has decorations such as a border, a title, and supports button components that close or iconify the window. Applications with a GUI usually include at least one frame.

Now we are presented with a GUI editor to edit our main window frame :-)

Inserting our J members

To add GUI elements, drag and drop from the "Pallet" pane on the right "Drop in a JButton, JTextField, JLabel, click the "Preview Design" button to preview your current design in a window... Note, we haven't written a single line of code, Netbeans has been doing that for us in the background when we are dragging in components and styling them etc, to change the text displayed in the buttons and labels right click the JButton and select "Edit text" you can now change the text to "Greet!" or anything you prefer. 

Ok, now we are going to write some code because we want our beautiful designed application to actually do something so we are going to add events to our JButton. To add a click event to our button simply double click the JButton you added earlier and NetBeans will link up the code and you will be presented with a function that you can add your code in, this function will fire when a user clicks on the button. We are going to create a simple alert box with the users typed name to greet them :-)

Source code view

To head back to your design view click the "Design View" button, don't worry about all the other code that you see now just simply type this in your function:

// TODO add your handling code here:
 javax.swing.JOptionPane.showMessageDialog(this,"Hi Welcome "+jTextField1.getText());

Once that is done run your program by clicking the "Run Project" button or F6 ;-).

run our program

Enter your name in the JTextField and click the JButton, an alert greeting you should appear :-), hopefully this will point you in the right direction in terms of your Java GUI programming, you can expand from this do some research online if you want to know more or leave comments below we can discuss.