0% found this document useful (0 votes)
12 views6 pages

Java Swing GUI Components Guide

The document provides an overview of Java's GUI toolkits, AWT and Swing, highlighting their differences, component hierarchy, and common components. It explains the lightweight nature of Swing, its pluggable look and feel, and includes example programs for JLabel, JButton, JTextField, and JTextArea. Additionally, it outlines the structure of Swing's panes and the event handling model.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Java Swing GUI Components Guide

The document provides an overview of Java's GUI toolkits, AWT and Swing, highlighting their differences, component hierarchy, and common components. It explains the lightweight nature of Swing, its pluggable look and feel, and includes example programs for JLabel, JButton, JTextField, and JTextArea. Additionally, it outlines the structure of Swing's panes and the event handling model.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SWING IN JAVA

1. Introduction

 AWT (Abstract Window Toolkit) and Swing are GUI (Graphical User Interface) toolkits in Java used
to build window-based applications.

 A GUI allows users to interact with an application using visual components like buttons, text fields,
labels, etc.

2. Difference between AWT and Swing

Feature AWT Swing

Package [Link] [Link]

Platform-dependent (uses native OS Platform-independent (written in


Nature
components) Java)

Lightweight/Heavyweight Heavyweight components Lightweight components

Look & Feel Native OS look Custom (Pluggable Look and Feel)

Follows Delegation Event Model


Event Handling Follows Delegation Event Model
(same)

MVC Architecture Not strictly MVC Follows MVC architecture

JButton, JLabel, JTextField, JTable,


Examples Button, Label, TextField
etc.

3. Component Hierarchy (AWT & Swing)

All GUI elements in Java inherit from [Link].

Hierarchy (simplified):

Object

└── Component

├── Container

│ ├── Window

│ │ ├── Frame

│ │ ├── Dialog

│ └── Panel

│ └── Applet / JPanel

└── (Various Components like Button, Label, TextField, etc.)

In Swing:

JComponent (extends [Link])

├── JLabel
├── JButton

├── JTextField

├── JTextArea

├── JPanel

├── JTable

└── JList

4. Panes in Swing

Diagram from Book.

Every Swing JFrame has several panes (layers) to manage components:

1. Root Pane (JRootPane) – Base container of all panes.

2. Content Pane – Where user components are added ([Link]()).

3. Layered Pane – When we want to take several components as a group, we attach them in the layered
pane.

4. Glass Pane – Transparent layer for capturing mouse events or drawing effects.

5. Common Swing Components

a. JLabel

 Displays text or image; non-editable.

 Constructor:
JLabel(String text)

 Example:

 JLabel lbl = new JLabel("Name:");

b. JButton

 Creates a clickable button.

 Constructor:
JButton(String text)

 Example:

 JButton btn = new JButton("Submit");

c. JTextField

 Single-line text input field.

 Constructor:
JTextField(int columns)

 Example:

 JTextField tf = new JTextField(20);


d. JTextArea

 Multi-line text area for input or display.

 Constructor:
JTextArea(int rows, int columns)

 Example:

 JTextArea ta = new JTextArea(5, 30);

 Remember: Swing = Lightweight + Pluggable Look & Feel

 Use JFrame instead of Frame, and always import [Link].*.

Here are simple and exam-ready Java Swing programs for each component — JLabel, JButton, JTextField,
and JTextArea.
Each example can be run directly in any Java IDE or terminal.

✅ 1. Program using JLabel

import [Link].*;

public class LabelExample {

public static void main(String[] args) {

JFrame frame = new JFrame("JLabel Example");

JLabel label = new JLabel("Welcome to Swing!");

[Link](80, 50, 200, 30); // x, y, width, height

[Link](label);

[Link](300, 200);

[Link](null);

[Link](true);

[Link](JFrame.EXIT_ON_CLOSE);

Output: A small window displaying the text “Welcome to Swing!”

✅ 2. Program using JButton

import [Link].*;

import [Link].*;
public class ButtonExample {

public static void main(String[] args) {

JFrame frame = new JFrame("JButton Example");

JButton button = new JButton("Click Me");

JLabel label = new JLabel("Button not clicked yet");

[Link](90, 80, 120, 30);

[Link](80, 30, 200, 30);

[Link](new ActionListener() {

public void actionPerformed(ActionEvent e) {

[Link]("Button Clicked!");

});

[Link](button);

[Link](label);

[Link](300, 200);

[Link](null);

[Link](true);

[Link](JFrame.EXIT_ON_CLOSE);

Output: When you click the button, the label text changes to “Button Clicked!”

✅ 3. Program using JTextField

import [Link].*;

import [Link].*;

public class TextFieldExample {

public static void main(String[] args) {

JFrame frame = new JFrame("JTextField Example");

JTextField textField = new JTextField();


JButton button = new JButton("Show");

JLabel label = new JLabel();

[Link](80, 50, 150, 25);

[Link](100, 90, 80, 30);

[Link](80, 130, 200, 25);

[Link](new ActionListener() {

public void actionPerformed(ActionEvent e) {

String text = [Link]();

[Link]("You typed: " + text);

});

[Link](textField);

[Link](button);

[Link](label);

[Link](300, 250);

[Link](null);

[Link](true);

[Link](JFrame.EXIT_ON_CLOSE);

Output: Type text in the field → Click “Show” → The label displays what you typed.

✅ 4. Program using JTextArea

import [Link].*;

import [Link].*;

public class TextAreaExample {

public static void main(String[] args) {

JFrame frame = new JFrame("JTextArea Example");

JTextArea textArea = new JTextArea();

JButton button = new JButton("Display");


JLabel label = new JLabel();

[Link](50, 40, 200, 100);

[Link](100, 150, 100, 30);

[Link](50, 190, 250, 30);

[Link](new ActionListener() {

public void actionPerformed(ActionEvent e) {

String text = [Link]();

[Link]("Typed: " + text);

});

[Link](textArea);

[Link](button);

[Link](label);

[Link](300, 280);

[Link](null);

[Link](true);

[Link](JFrame.EXIT_ON_CLOSE);

You might also like