In this article Components, Hierarchy, and Panes  we give the information about Components, Hierarchy, and Panes are the major parts of creating GUI (Graphical User Interface) in Swing.

Components, Hierarchy, and Panes are the major parts of creating GUI (Graphical User Interface) in Swing. Swing is an advanced version of AWT (Abstract Window Toolkit), which provides more flexible and better user interface.

Components Hierarchy and Panes In Swing:

Components

Components in Swing are the user interface elements with which the user interacts. These are similar to the components of AWT, but more advanced and modern versions are available in Swing. All Swing components are derived from JComponent class.

Major Swing Components:

JButton:

It is a clickable button.

Example:

JButton button = new JButton(“Click Me”);

JLabel :

It is used to display a static text or image.

Example:

JLabel label = new JLabel(“Welcome to Swing”);

JTextField:

It is used to take text input from the user.

Example:

JTextField textField = new JTextField(20);

JCheckBox:

It gives the user the option to select or skip an option.

Example:

JCheckBox checkBox = new JCheckBox(“I agree to the terms”);

JRadioButton:

It is used to select an option where only one option can be selected from a group.

Example:

JRadioButton radioButton = new JRadioButton(“Male”);

JComboBox:

This is a dropdown list from which the user can choose an option.

Example:

JComboBox comboBox = new JComboBox(new String[]{“Option 1”, “Option 2”, “Option 3”});

JList:

It gives the option to choose one or more options from many options.

Example:

JList list = new JList(new String[]{“Item 1”, “Item 2”, “Item 3”});

Advantages of Swing Components:

Platform-independent look and feel: Swing components look the same on every platform, whereas in AWT it was platform dependent.

Lightweight components: Swing components are lightweight as they are written entirely in Java and have low dependency on the operating system.

Pluggable look and feel: You can change the look and feel of Swing components.

Hierarchy:

Components in Swing are arranged under a particular hierarchy. This hierarchy is called Component Hierarchy. It consists of Top-level Containers and other components within them, which form the user interface.

Swing Component Hierarchy:

Top-level Containers:

These are the components that host other components of Swing. There are three major top-level containers in Swing:

JFrame:

It is a normal window in Swing, containing a menu bar, title, border, etc.

JDialog:

It is a window used to display information or take input from the user. It appears on top of the main window (JFrame).

JApplet:

This is an applet that runs in a browser or applet viewer.

Intermediate Containers:

These are containers that are used to group other components. The most common intermediate container in Swing is a JPanel. Other components can be added inside a JPanel.

JPanel:

It is a simple container used to hold and organize other components together.

Basic Components:

These are the actual components of the user interface, such as buttons, labels, text fields, etc. They are placed inside the panel or top-level container.

Example of Hierarchy:

JFrame frame = new JFrame(“Swing Example”);

JPanel panel = new JPanel();

JButton button = new JButton(“Click Me”);

JLabel label = new JLabel(“Hello”);

panel.add(button);  // button join to panel

panel.add(label);   // button join to lable

frame.add(panel);   // frame jjoin to lable

frame.setSize(300, 200);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

In this example, JFrame is the top level container to which a JPanel is added and JButton and JLabel are placed inside the panel. This shows the component hierarchy of Swing.

Panes

Panes in Swing are used to organize different components and display them at different levels. Every JFrame contains one or more panes, which help in handling the components in a structured manner.

Main types of Panes:

Content Pane :

This is the most important pane where all the UI components are added.

Using JFrame.getContentPane() we can get the content pane and add components to it.

Example:

JFrame frame = new JFrame();

Container contentPane = frame.getContentPane();

contentPane.add(new JButton(“Click Me”));

Glass Pane :

It is a transparent pane on top of the top-level container. It is used to display special events or overlay elements.

It is usually used for event tracking or a special effect.

Example:

frame.setGlassPane(new JPanel() {

    public void paintComponent(Graphics g) {

        g.setColor(new Color(0, 0, 0, 50));

        g.fillRect(0, 0, getWidth(), getHeight());

    }

});

frame.getGlassPane().setVisible(true);

Layered Pane :

This allows components to be added on different layers. You can place one component on top of another.

Components can be arranged on different levels using JLayeredPane in Swing.

Example:

JLayeredPane layeredPane = new JLayeredPane();

JButton topButton = new JButton(“Top Button”);

JButton bottomButton = new JButton(“Bottom Button”);

topButton.setBounds(20, 20, 100, 50);

bottomButton.setBounds(40, 40, 100, 50);

layeredPane.add(bottomButton, JLayeredPane.DEFAULT_LAYER);

layeredPane.add(topButton, JLayeredPane.PALETTE_LAYER);

frame.add(layeredPane);

Root Pane :

It is the pane that holds all the panes of the JFrame together. It is defined as JRootPane and consists of Content Pane, Glass Pane, and Layered Pane.

Usage of Panes:

In Content Pane we add common UI components.

Glass Pane is used for overlays or special effects.

Layered Pane is used for overlapping components.

Root Pane manages all the panes.

Example: Simple Swing Application

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

public class SimpleSwingApp

{

          public static void main(String[] args)

          {

          // Create a new frame

          JFrame frame = new JFrame(“Simple Swing Application”);

                   // Create a button

          JButton button = new JButton(“Click Me”);

                                      // Create a panel and add the button to it

                   JPanel panel = new JPanel();

          panel.add(button);

                   // Set up the frame

          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          frame.add(panel);

          frame.setSize(300, 200);

          frame.setVisible(true);

          }

}

Summary:

  1. Components: Components in Swing such as JButton, JLabel, JTextField, etc. are the main part of the user interface and all of them are derived from JComponent.
  2. Hierarchy: Components in Swing are under a structured hierarchy, in which JFrame is the topmost container and other components such as JPanel and JButton are at the lower levels.
  3. Panes: Swing uses various panes such as Content Pane, Glass Pane, Layered Pane, and Root Pane to manage and organize components.

Creating and managing GUIs in Swing is more flexible and user-friendly than AWT, making it easier to create complex interfaces.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *