Introduction To Java Swing
Introduction To Java Swing
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
05
02
Rich set of components: Swing
provides a wide range of pre-built
components for building user
interfaces.
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.
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
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
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 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 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
add(new JLabel("Username:"));
add(usernameField);
add(new JLabel("Password:"));
add(passwordField);
add(new JLabel());
add(loginButton);
loginButton.addActionListener(this);
// ActionListener method
Basic Login Form Example
} else {
new LoginFrame().setVisible(true); }
Examples of Swing Applications
04 02
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. 😄