Java Language provides two Graphical User Interface (GUI) packages.

  • java.awt
  • javax.swing

The java.awt package was the primary repository for classes that are used to create a GUI in java but many of the classes it defines have been superseded in Java 2 by javax.swing. However the Swing classes are generally derived from, and depend on, fundamental classes within java.awt, so these cant be ignored. Following picture describes class diagram of java.wt package.

Similarly, following picture depicts the class diagram of javax.swing package.

Components in swing package

A component represents a graphical entity of one kind or another that can be displayed on screen. All container and visual components inherit from java.awt.Component. There are some key Classes that are used frequently for example Window, JFrame, JDialog, JApplet etc.

Following are few examples that will describe how to create a GUI object in java.


// Create a button with text OK
Jbutton   jbtOK = new JButton("OK");

// Create a label with text "Enter your name: "
Jlabel    jlblName = new JLabel("Enter your name: ");

// Create a text field with text "Type Name Here"
JTextField   jtfName = new JTextField("Type Name Here");

// Create a check box with text bold
JCheckBox   jchkBold = new JCheckBox("Bold");

// Create a radio button with text red
JRadioButton   jrbRed = new JRadioButton("Red");

// Create a combo box with choices red, green, and blue
JComboBox jcboColor = new JComboBox(new String[]{"Red",  "Green", "Blue"});


Frame is a window that is not contained inside another window. Frame is the basis to contain other user interface components in Java GUI applications. The JFrame class can be used to create windows. For Swing GUI programs, use JFrame class to create widows.

import javax.swing.JFrame;
public class TryWindow {
  // The window object
  static JFrame aWindow = new JFrame("This is the Window Title");
  public static void main(String[] args) {
    int windowWidth = 400;             // Window width in pixels
    int windowHeight = 150;            // Window height in pixels
    // set position and size
   aWindow.setBounds(50, 100,windowWidth, windowHeight);
    aWindow.setVisible(true);          // Display the window

Another Example:

import javax.swing.*;
public class MyFrame {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Test Frame");
    frame.setSize(400, 300);

Adding Components into a Frame

// Add a button into the frame
frame.getContentPane().add(new JButton("OK"));

Methods for Components

There are several methods that are available for components

  •  setForeground( ) and setBackground( )
  • setFont( )
  • setEnabled( )
  • setSize( ) and setBounds( )
  • setVisible( )


The Container class is abstract and most commonly used concrete subclasses are JApplet, JFrame, JDialog and Panel. All the classes derived from the Container can contain other objects of the classes derived from Component. Swing provides containers such as

  • top level: frames, dialogs
  • intermediate level: panel, scroll pane, tabbed pane, …
  • other Swing components: buttons, labels, …

It uses a layout manager to position and size the components contained in them. Components are added to a container using one of the various forms of its add method depending on which layout manager is used by the container. For example, panel.add(component)

Top Level Containers

Every program that presents a Swing GUI contains at least one top-level container. A Top level container provides the support that Swing components need to perform their painting and event-handling. Swing provides three top-level containers:

  • JFrame (Main window)
  • JDialog (Secondary window)
  • JApplet (An applet display area within a browser window)

Layout Managers

Java’s layout managers provide a level of abstraction to automatically map your user interface on all window systems. The UI components are placed in containers. Each container has a layout manager to arrange the UI components within the container.
Click here to read more about layout managers in Java

Tagged with: ClassesJAVAJAVA GUIObject Oriented

4 Responses to JAVA Graphical User Interface (GUI)

  1. shashank says:

    nice on the web…gr8 website…loved it

  2. […] JAVA Graphical User Interface (GUI) | Java Programming … click here … […]

  3. plzz help me i want to design a GUI based application with best UI,
    so what i need to do.???? help me plzzz

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.