0% found this document useful (0 votes)
7 views

Introduction To Java Swing

Uploaded by

hasnatrasool163
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Introduction To Java Swing

Uploaded by

hasnatrasool163
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Introduction to Java Swing

This presentation provides an overview of the Java Swing GUI toolkit.


What is Java Swing?

Java Swing is a set of GUI Swing is part of the Java


components and tools for 02 Foundation Classes (JFC) and
building graphical user is platform-independent.
interfaces in Java.

01 03
It provides a rich set of It allows developers to create
features and functionality for applications that can run on
creating interactive and different operating systems
visually appealing 04 without any modifications.
applications.
Advantages of Java Swing

Cross-platform compatibility: Swing


Customizable look and feel: Swing
applications can run on any platform
allows developers to customize the 01
that supports Java.
look and feel of their applications.

05
02
Rich set of components: Swing
provides a wide range of pre-built
components for building user
interfaces.

Support for internationalization: 04


Swing supports the display of text in 03
different languages and character Event-driven architecture: Swing
encodings. applications respond to user actions
and events, providing a responsive
and interactive user experience.
Getting Started with Swing

● To start using Swing, you need to have the Java


Development Kit (JDK) installed on your computer.
● You can create Swing applications using an
integrated development environment (IDE) such as
Eclipse or IntelliJ IDEA.
● Swing applications are typically built using the
Model View-Controller (MVC) design pattern.
● You can create GUI components using the Swing
API and add them to a container such as a JFrame or
JPanel.
Common Swing Components

JButton: A clickable button that


JTextField: A text input field for user
performs an action when clicked.
input. 01

05
JTextArea: A multi-line text input 02
JLabel: A component for displaying
field.It supports word- wrap,scrolling
text or an image.
and text selection.

JTable: A component to display 04


data in graphical user interface , 03
features like sorting , filtering JCheckBox: A checkbox for selecting
and custom rendering oells options.It allows toggle between
supported. checked and unchecked states
Introduction to MVC in Java Swing:
Model-View-Controller (MVC) is a design pattern commonly used in software development to organize applications into three
interconnected components: the Model, the View, and the Controller. In Java Swing, MVC is crucial for structuring applications to
enhance maintainability, scalability, and reusability.

The Model represents the The View represents the presentation The Controller acts as an
application's data and business layer of the application. It defines the intermediary between the Model
logic. It encapsulates the state of the user interface elements that users and the View.It receives user input
application and provides methods to interact with, such as buttons, text from the View, processes it, and
access and manipulate this data. For fields, and tables. For instance, in the
updates the Model accordingly.In
example, in a student management student management system, the
Java Swing, Controllers are often
system, the Model would store View would display the student
information about students, such as information in a table format,
implemented as event listeners
their names, grades, and attendance allowing users to view and edit the attached to user interface
records. data. components.
Layout Managers

Each layout manager has its own Layout managers are used to
rules for positioning and resizing arrange and position components
components. 01 within a container.

04 02

Layout managers provide Some commonly used layout


flexibility in creating responsive managers in Swing are
03
and dynamic user interfaces. BorderLayout, GridLayout, and
FlowLayout.
Layout Managers

01

04 02

03
Event Handling

01 Listeners can be registered with components to 02 In Swing, events are generated when a user
receive events and trigger appropriate interacts with a component, such as clicking a
responses. button.

03 Event listeners are used to handle these events 04 Swing provides a comprehensive set of listener
and perform the desired actions. interfaces for different types of events.
Swing vs AWT

● Swing is the successor to the Abstract Window


● AWT is the original GUI toolkit for Java and
Toolkit (AWT) in Java.
● provides a basic set of components.
Built entirely in java, offering consistent
● Offers a limited set of components and lack
behaviour across platforms.
● advanced features.
Swing provides a more powerful and flexible set
● Lacks customization and depend on natives
of components with a consistent look and feel.
● Being an older technology lacks modern features.
● Provides a wide range of customizations.
● Awt has included in jdk 1.0 version
● Swing was added in jdk 1.2 version.
● Components are heavyweights that causes issues
● Unlike AWT, Swing components are lightweight
sometime.
and have more features and customization
options.
Hierarchy of Java Swing classes
The hierarchy of java swing API is given below.
Tips for Building Swing Applications

Use layout managers to Use appropriate event listeners


ensure proper arrangement of 02 to handle user interactions.
components.

01 03
Test your application on
different platforms to ensure
It is better approach to use
compatibility and consistency.
layout managers they will
auto manage components on 04
resize window
JFrame vs JPanel usecase

JFrame: The Foundation JPanel: Dividing the Screen

JFrame is a component of Java Swing that serves as To create complex layouts, developers often use
the main window or container for building graphical JPanel on top of JFrame. JPanel acts as a container
user interfaces. It provides the basic structure and within the JFrame, allowing for the division of the
functionality for creating windows, handling events, screen into multiple sections or areas. This makes it
and managing components. easier to organize and manage different components
and create visually appealing and interactive user
interfaces.
JFrame useful methods

➔ setTitle(“Frame”);
➔ setSize(500,500); -> width , height
➔ setResizable(boolean ) -> true,false
➔ getContentPane.setBackground(Color.WHITE);
➔ setLocation(200,100);
➔ setDefaultCloseOperation(int); -> EXIT_ON_CLOSE
➔ setLayout(null);
➔ setForeground(Color.GREEN);
➔ repaint();
➔ pack();
JPanel useful methods

1. add(Component 4. 7.setVisible(boolea
comp): Adds a setBackground(Color n visible): Sets the
component to the bg): Sets the background visibility of the panel.
panel. color of the panel.
2. remove(Component
comp): Removes a
8.setEnabled(boolea
5. setOpaque(boolean
component from the n enabled): Sets
isOpaque): Sets whether
panel. whether this panel is
this panel is opaque.
3. setLayout(LayoutM enabled.
anager manager):
6. setBorder(Border
Sets the layout
border): Sets the border 9.repaint();
manager for the panel.
of the panel.
Event Handling in Swing .

1 2 3

In Java, event listeners are interfaces The ActionListener interface is used to Similarly, the MouseListener interface is
that allow developers to handle user handle events triggered by buttons, used to handle mouse-related events such
interactions with graphical user menu items, or other components. as clicking, hovering, or dragging.
interfaces (GUIs) or other input devices. By implementing the actionPerformed Developers can override methods like
Some commonly used event listener method, developers can define the mouseClicked or mouseEntered to define
interfaces include ActionListener, actions to be performed when an event specific actions based on user interactions.
MouseListener, and KeyListener. occurs. For example, clicking a button The KeyListener interface, on the other
to submit a form or selecting a menu hand, is used to handle keyboard events
item to open a new window. like key presses or releases. By
implementing methods like keyPressed or
keyReleased.
Basic Login Form Example

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class LoginFrame extends JFrame implements ActionListener {

private JTextField usernameField;

private JPasswordField passwordField;

private JButton loginButton;

public LoginFrame() {

setTitle("Login");

setSize(300, 150);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Basic Login Form Example

public LoginFrame() {

setTitle("Login");

setSize(300, 150);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create components

usernameField = new JTextField(15);

passwordField = new JPasswordField(15);

loginButton = new JButton("Login");

// Add components to the frame

setLayout(new GridLayout(3, 2));


Basic Login Form Example

add(new JLabel("Username:"));

add(usernameField);

add(new JLabel("Password:"));

add(passwordField);

add(new JLabel());

add(loginButton);

// Add action listener to the login button

loginButton.addActionListener(this);

// ActionListener method
Basic Login Form Example

public void actionPerformed(ActionEvent e) {

String username = usernameField.getText();

String password = new String(passwordField.getPassword());

// Check if username and password are valid

if (username.equals("admin") && password.equals("admin123")) {

JOptionPane.showMessageDialog(this, "Login successful!");

} else {

JOptionPane.showMessageDialog(this, "Invalid username or password. Please try again.");

}public static void main(String[] args) {

new LoginFrame().setVisible(true); }
Examples of Swing Applications

Graphical IDEs: IDEs like Eclipse Text Editors: Applications like


or IntelliJ IDEA use Swing for Notepad or Sublime Text are built
their graphical interfaces. 01 using Swing.

04 02

Games: Many games, such as Media Players: Applications like


Minecraft, use Swing for their VLC or Windows Media Player
03
graphical interfaces. use Swing for their user interfaces.
Conclusion

01 With Swing, developers can create applications 02 Java Swing is a powerful GUI toolkit for
that run on different operating systems without building interactive and visually appealing
any modifications. applications.

03 It provides a rich set of components, 04 Swing is widely used in various domains, from
customizable look and feel, and cross-platform desktop applications to games and IDEs.
compatibility.
Thank you. Please feel free to ask any questions. 😄

You might also like