Road to build a Desktop Application Using JAVA

Nitish Gattepalli
8 min readApr 24, 2019

--

A lot of people know how to code in Java programming language. Java is used to build robust and platform independent applications in many domains. This language is used for developing Android applications, web applications, desktop applications and many more. Also, it is the most preferred language to be learnt among students and professionals. Many companies require java developers for building software applications.

This article describes the procedure to build a desktop application using Java and is for those audience who have little experience in writing java programs, but haven’t built any application and are willing to build one. If you are such a person, continue reading this article and get to learn the tools and techniques for building your first desktop application using Java.

You can create desktop applications that can have great functionality, attractive designs on user interfaces, and that can store, retrieve and display data to the user from the database by using the tools and techniques mentioned in this article. Applications such as library management system, attendance management system, and billing system are a few examples which you can build after reading this article.

I have divided this article into sections that describe the tools and concepts to be used, the software platforms required and finally the procedure to build a Java desktop app.

The below-mentioned are the tools, concepts, and platforms which you will be using to build the application.

Tools/Concepts/Techniques:

1. Core Java

2. Java Swing

3. MySQL

4. JDBC

Software/Platforms:

  1. NetBeans IDE
  2. MySQL

Procedure:

Follow the steps below. They describe briefly about every tool and technique required to build a desktop application and also refer the helpful links.

1. Downloading JDK

Make sure that you have JDK installed on your computer. Otherwise, you cannot run java programs and develop Java applications using NetBeans.

Download it here.

2. Download the NETBEANS IDE and install it

I prefer NetBeans because it allows programmers to simply drag and drop the elements such as buttons, text fields and labels onto the user interface. Whereas, in other IDE’s such as Eclipse you have to write the code for placing that element on the page. It consumes a lot of time and makes the building process complex. Thus, NetBeans enables us to build applications faster.

Download it here.

3. Applying Java Swing for creating user interfaces.

Java Swing is a toolkit for building Graphical User Interfaces (GUI’s). It is a part of the Java Foundation Classes (JFC) which contain packages for building window-based applications or desktop applications. Swing is a better version to the Abstract Windowing Toolkit (AWT) of JFC because swing contains components that are platform independent, lightweight and have complex features compared to AWT. I will discuss the basic components of swing below that are fundamental to creating a desktop application while also explaining the procedure for building the app.

4. Start by creating a new project

Create new project and create a JFrame class in NetBeans. JFrame is the window or container or the user-interface where you can place buttons, labels, text fields, images, check-boxes, tables and lot more.

5. Adding components to the window

After creating a new JFrame, you can start inserting the swing components to your JFrame by simply dragging the available components present at the top right side of your screen. Some of the essential components of a user interface are discussed below:

JPanel- It can be considered as a sub-container of the JFrame. You can make it hold other components such as JLabels, JButtons, Jlabels and more.

JLabel- It is used to place single line text inside the JFrame which user cannot edit. You can add labels to components such as JTextFields, JButtons, and JTables using JLabel.

JTextField- The JTextField is an empty box which allows users to input a single line of text. You can collect user information using this component.

JList- This comes handy when you want to provide a list of items to the user from which they can select one option. As an example, you can use this to insert a list of books available in the library.

JButton- Buttons are an essential component for every application. The JButton class can be used to insert a button onto the frame and make it perform any action.

JComboBox- Similar to JLists you can use JComboBox for creating a drop-down menu of items. JComboBox is helpful when you want to display a list of choices to the user while also saving the space taken by it.

JRadioButton- Radio button is a common component of every user interface, may it be a web app, desktop app or mobile app. JRadioButton allows you to place multiple options on your JFrame from which a user can select any one.

JPasswordField- Password fields show up solid dots when you enter your password. The JPasswordField is one such component for inserting single-line text boxes to enter passwords.

JTable- JTable is very much essential when you want to display data to the user in the form of a table composed of rows and columns. For example, you may wish to display the list of books available in the library to the user.

JFileChooser- If you think that the user needs to select a file from which your application should perform specified actions then, JFileChooser is the only choice. All the notepad applications have this feature for viewing the files saved in the past.

JOptionPane- It is a dialog box which pops up to display a message to the user or request user to enter data. They are traditionally used when an alert or warning is to be given to the user.

6. Using properties box

The properties box becomes visible at the bottom-right of your screen when you select a swing component. It contains properties that allow you to place, size and style those components.

7. Structuring components on the window

Just as how parts of any document or presentation are structured, the JFrame components should be also be laid out according to the requirement. The java layout managers allow you to place the components of a JFrame in a particular format. The various layout managers provide different ways to arrange the components as per your desire. For example, if you are willing to arrange a set of elements in a line then you can utilize the FlowLayout class which places them one after the other in a flow. Few of the layout managers are BorderLayout, GridLayout, BoxLayout, and GridBagLayout. Right click on the JFrame, go to set layout option, and choose your preferred layout.

8. Making the components to perform actions

After completing the designing part of the application, it is required to enable the components to work as desired. You may want to navigate to a different frame when a user clicks on a button, or the information typed by the user in the textbox should be captured, stored and displayed later, or a caution message should be displayed when the user enters the wrong password. To make your application perform such tasks you will have to learn the methods associated with those components and write appropriate Java code. Click the “Source” option at the top of class file to manipulate the source code.

Refer this website to view the available Java Swing components, their methods, and code snippets.

9. Downloading MySQL

The application is not dynamic until it has the potential to manipulate users data. For example, if you want to store the information about the books a person issued in the library, then your application should store this data into a place where it can be retrieved in the future. It is necessary to have little knowledge about databases and query language to make your application dynamic.

All the data is stored in a place called database. There are many databases available such as Oracle Database, MySQL, PostgreSQL, and MS SQL Server. Using any one of these databases, you can give your application the data storage extension. Though these databases have few differences and similarities, I have mentioned about MySQL. You can also use any other database for your application. Install any one of the database platforms mentioned above to start using it.

Install MySQL here.

We interact with the database through a query language called as Structured Query Language (SQL). We provide statements to the database which are interpreted to perform specified action such as creating a table, updating data or removing data.

Refer this website for MySQL tutorial.

After finishing the set-up open the MySQL Command Line Client and log in with the password which was set during the installation. Also, you can use the GUI version i.e. the MySQL Workbench. Then, create a database for your application, create tables in that database and insert required fields. Learn the MySQL statements for performing such actions and also query statements such as INSERT, SELECT, DELETE, and UPDATE for manipulating the data.

10. Applying JDBC

It is now required to connect your Java application to the database created. This task is accomplished by using a Java API known as Java Database Connectivity (JDBC). Applying this, you can access and manipulate data in your database through the interfaces created earlier using java swing. In simple words, for example, the information entered by a user in a textbox can be stored into the targeted table of the database when a submit button is clicked, using JDBC.

11. Integrating the GUI and database

You can access and manipulate data in the database through firstly registering a driver for interaction and then establishing a connection with the database by writing a few lines of code. Later, you can start inserting statements or MySQL queries in the java code that executes indirectly in the database. Use relevant classes, interfaces, and methods of JDBC API for storing and extracting data.

Refer this link for more information on using JDBC.

12. Creating Jar file

Finally, after finishing all these procedures, it is time to aggregate all the classes, images and other files of the application into one single file called the JAR file. This jar file opens the main class of your application in the form of a GUI whenever you click it. For creating the jar file, in Netbeans

  1. Right-click on the Project name
  2. Select Properties
  3. Click Packaging
  4. Check Build JAR after Compiling
  5. Check Compress JAR File
  6. Click OK to accept changes
  7. Right-click on the Project name again
  8. Select Build or Clean and Build

Find the .jar file in the dist sub-folder of your project folder.

And that is it! Congratulations…You have built your first desktop application using Java.

Click here to view the code of Attendance Repository desktop application on GitHub which I built using Java.

--

--

Nitish Gattepalli
Nitish Gattepalli

Written by Nitish Gattepalli

I talk about Technology & Software Engineering

Responses (2)