This is saif, enjoying cool evening with a cup of tea and writing this tutorial titled as “Getting Started with GUI based Java Applications using NetBeans”. This article is for beginners who want to get their journey started in programming world with GUI Applications using Java. It will also be helpful for those who are already familiar with Java or some other programming language and want to design GUI based java Applications.
This article will guide step by step how to make a GUI Application in Java using Netbeans as an IDE. Let’s Begin.
If you have no prior knowledge of Java and NetBeans read this article once to have a flavor.
Getting Started with NetBeans
- Download and install Java from here
- Download and install NetBeans from here
- Open NetBeans and Create a new project
- Select java -> Java Application from the dialog and then click next
- Name your new project, uncheck ‘create main class’ option and click on finish
- Create a new package by right clicking on ‘source package’ as:
new -> Java Package
- Name the Package and click finish
- After creating a new package, create a new ‘JFrame Form’ by right clicking on new package created (named as GUI):
new -> JFrame Form
- Name the new JFrame and click Finish
- After performing the above steps, you will get the following window.
- Project Window: Contains the project files in hierarchical order
- Navigator: Provides hierarchical representation of all the project components
- Palette: Contains the list of all GUI Components arranged in categories
- Design Area: It is the window provided to create and edit GUI Forms. You can drag and drop components from palette here to design a GUI Application.
- Properties Window: It displays all the properties and available events for currently selected Component in GUI builder or navigator
- Press F6 or click Debug to execute the program. You will get an empty Frame
- First of all, we will place a Title on the Frame. We have two methods to achieve this. Either right clicking on Frame in Design Area and selecting Properties Window or directly changing the title property in Properties Window located on bottom right.
- Now after executing, our frame will look like this.
- Let’s start with out first GUI Application. Drag and drop a Panel from Palette to the Design Form. Stretch it to the whole form.
- Right click on Panel and set layout to ‘Absolute Layout’
- Drag and drop one JLabel, one JTextField and one JButton on the Design form and arrange them as follows.
- You can also select a single or multiple components form navigator to change its properties
- Change JLabel text property to “Enter Your Name:”, JTextField to null and JButton to “Submit” using property window
- Change the variable names for Jlabel, JTextField and JButton to nameLabel, nameField and submit respectively by right clicking on each component and choosing Change Variable Name.
- After changing components’ variable name, you can see the changes in navigator window.
- Double click submit button on Design Form and you will be taken to the code view of this Form. You have just created a Listener for submit button. This Listener will called when you will press submit button after executing your Application.
- Now we will place these two lines of code in it.
String name = nameField.getText();
JOptionPane.showMessageDialog(this, "Hello "+name+ "! Here is Your First GUI Application");
- After completing above steps, press F6 to execute you Application. Enter you name in the text Field and press submit. You will get the following message-box.
- To make ‘.jar’ file of you project, click on ‘clean and built project’.
- After successful build, you will get this output
- Then go to NetBeans Project folder. You will get the jar file inside dist folder
You can directly execute you jar files.
We are done with setting up Java and netbeans and successfully executing our first GUI based Java Application.
Stay with Protorials for more tutorials.
Have a nice day!