Technology Careers
HDFC Data Digits HDFC ERGO Technocrat Kotak Mahindra Bank-PGP in Full Stack Software Engineering CSB Bank-Digital Transformation Skills Program FreeCharge by Axis Bank - FinTech Engineering Program Post Graduate Program in Full Stack Software Engineering
Banking and Finance Careers
Axis Bank – Priority Banking Program Axis Bank Young Bankers Program HDFC - Relationship Manager Axis Bank - Virtual Sales & Relationship Management Program Excelerate - Wealth Manager Program HDFC - Certification in Virtual Relationship Management HDFC- Trade Finance Operations IndusInd Bank – Banking Sales and Business Development Bajaj Finserv – Beyond Program
Add To Bookmark

How to Create a GUI with Java


By NIIT Editorial

Published on 27/07/2021

8 minutes

GUI, which stands for Graphical User Interface, is a user-friendly visual experience builder for Java applications. It comprises graphical units like buttons, labels, windows, etc. via which users can connect with an application. Swing and JavaFX are two commonly used applications to create GUIs in Java.

Elements of GUI:

A GUI comprises an array of user interface elements. All these elements are displayed when a user is interacting with an application and they are as follows:

1.  Input commands such as buttons, check boxes, dropdown lists and text fields.

2.  Informational components like banners, icons, labels or notification dialogs.

3.  Navigational units like menus, sidebars and breadcrumbs.

GUI in JAVA: Swing and JavaFX

As mentioned above, to create a GUI in Java, Swing and JavaFX are the most commonly used applications. Swing was designed with a flexible architecture to make the elements customizable and easy to plug-and-play which is why it is the first choice for java developers while creating GUIs.

As far as JavaFX is concerned, it consists of a totally different set of graphic components along with new features and terminologies.

 Creating a GUI

The process of creating a GUI in Swing starts with creating a class that represents the main GUI. An article of this class acts as a container which holds all the other components to be displayed.

In most of the projects, the main interface article is a frame, i.e., the JFrame class in javax.swing package. A frame is basically a window which is displayed whenever a user opens an application on his/her computer. It has a title bar and buttons such as minimize, maximize and close along with other features.

The JFrame class consists of simple constructors such as JFrame() and JFrame(String). The JFrame() leaves the frame’s title bar empty, whereas the JFrame(String) places the title bar to a specified text.

Apart from the title, the size of the frame can also be customized. It can be established by incorporating the setSize(int, int) method by inserting the width and height desired for the frame. The size of a frame is always designated in pixels.

For example, calling setSize(550,350) would create a frame that would be 550 pixels wide and 350 pixels tall.

Usually, frames are invisible at the time of their creation. However, a user can make them visible by using the frame’s setVisible(boolean) method by using the word ‘true’ as an argument.

The following are the steps to create GUI in Java 

STEP 1: The following code is to be copied into an editor

 

import javax.swing.*;

class gui{

public static void main(String args[]){

     JFrame jframe = new JFrame("GUI Screen");   //create JFrame object

     jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     jframe.setSize(400,400);        //set size of GUI screen

     jframe.setVisible(true);

}

}

 

STEP 2: Save and compile the code as mentioned above and then run it.

STEP 3: Adding buttons to the above frame. To create a component in Java, the user is required to create an object of that component’s class. We have already understood the container class JFrame.

 

One such component to implement is JButton. This class represents the clickable buttons. In any application or program, buttons trigger user actions. Literally, every action begins with a click; like to close an application, the user would click on the close button.

 

A swing can also be inserted, which can feature a text, a graphical icon or a combination of both. A user can use the following constructors:

 

·          JButton(String): This button is labelled with a specified text.

·         JButton(Icon): This button is labelled with a graphical icon.

·         JButton(String,Icon): This button is labelled with a combination of text and icon.

 

The following code is to be copied into an editor:

import javax.swing.*;

class gui{

    public static void main(String args[]){

        JFrame jframe = new JFrame("GUI Screen");   //create JFrame object

        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jframe.setSize(400,400);            //set size of GUI screen

        JButton pressButton = new JButton("Press");  //create JButton object

        jframe.getContentPane().add(pressButton);

        jframe.setVisible(true);

    }

}

 

STEP 4: The above is to be executed. A big button will appear on the screen.

 

 

STEP 5: A user can add two buttons to the frame as well. Copy the code given below into an editor.
 

import javax.swing.*;

 

class gui{

 

    public static void main(String args[]){

 

        JFrame jframe = new JFrame("GUI Screen");

 

        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

        jframe.setSize(400,400);

 

        JButton firstButton = new JButton("First Button");  //create firstButton object

 

        JButton secondButton = new JButton("Second Button");  //create secondButton object

 

        jframe.getContentPane().add(firstButton);

 

        jframe.getContentPane().add(secondButton);

 

        jframe.setVisible(true);

 

    }

 

}

 

STEP 6: Save, compile and run the above code. 

STEP 7: Unpredicted output = ? It means that the buttons are getting overlapped. 

STEP 8: A user can create chat frames as well. Below is an example of the same: 

 

The following is the code for creating a chat frame:

 

import javax.swing.*;

 

import java.awt.*;

 

class gui {

 

    public static void main(String args[]) {



 

        //Create the Frame

 

        JFrame jframe = new JFrame("Chat Screen");

 

        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

        jframe.setSize(400, 400);



 

//     create two menubar button FILE and HELP

 

        JMenuBar menuBar = new JMenuBar();

 

        JMenu fileMenu = new JMenu("FILE");

 

        JMenu helpMenu = new JMenu("Help");

 

        menuBar.add(fileMenu);

 

        menuBar.add(helpMenu);



 

//      create two more option in FILE button

 

        JMenuItem fileMenu1 = new JMenuItem("new file");

 

        JMenuItem fileMenu2 = new JMenuItem("Save as");

 

        fileMenu.add(fileMenu1);

 

       fileMenu.add(fileMenu2);



 

        // Text Area at the Center

 

        JTextArea textArea = new JTextArea();



 

        //Create the panel at bottom and add label, textArea and buttons

 

        JPanel panel = new JPanel(); // this panel is not visible in output

 

        JLabel label = new JLabel("Please Enter Text");

 

        JTextField textField = new JTextField(15); // accepts upto 15 characters

 

        JButton btn_send = new JButton("Send");

 

        JButton btn_reset = new JButton("Reset");

 

        panel.add(label); // Components Added using Flow Layout

 

        panel.add(textField);

 

        panel.add(btn_send);

 

        panel.add(btn_reset);







 

        //Adding Components to the frame.

 

        jframe.getContentPane().add(BorderLayout.SOUTH, panel);

 

        jframe.getContentPane().add(BorderLayout.NORTH, menuBar);

 

        jframe.getContentPane().add(BorderLayout.CENTER, textArea);

 

        jframe.setVisible(true);

 

    }

 

}

 

Conclusion

It can be concluded that creating GUI in Java is a very easy and user-friendly process as the Java applications provide customization according to the requirements of the user. To learn more about Java, and to become an expert in Java development go to NIIT and check out the courses that give great insight on Java.



PGP in Full Stack Product Engineering

Become an Industry-Ready StackRoute Certified Full Stack Product Engineer who can take up Product Development and Digital Transformation projects. Job Assured Program* with a minimum CTC of ₹7LPA*

Job Assured Program*

Easy Financing Options

Top