0% found this document useful (0 votes)
3 views17 pages

Java Unit 4

The document outlines the syllabus for Java Semester 4, focusing on Swing components, containers, layout managers, and the event delegation model. It provides examples and explanations for various components like JLabel, JButton, JTextField, and layout managers such as FlowLayout and GridLayout. Additionally, it details event handling in Java, including event sources, listeners, and specific mouse and keyboard events.

Uploaded by

xatiy52541
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

Java Unit 4

The document outlines the syllabus for Java Semester 4, focusing on Swing components, containers, layout managers, and the event delegation model. It provides examples and explanations for various components like JLabel, JButton, JTextField, and layout managers such as FlowLayout and GridLayout. Additionally, it details event handling in Java, including event sources, listeners, and specific mouse and keyboard events.

Uploaded by

xatiy52541
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

JAVA

SEMESTER 4
UNIT - 4

HI COLLEGE
SYLLABUS
UNIT - 4

MAHIYA SAY 'JAVA JAVA'

HI COLLEGE
COMPONENTS

JLabel and ImageIcon


`JLabel` is used to display text or an image on the screen. `ImageIcon` is used to
represent an image. Here's an example that shows how to use `JLabel` and `ImageIcon`
together:

HI COLLEGE 01
COMPONENTS
Buttons (JButton, JToggleButton, JCheckBox,
JRadioButton)
Swing provides various types of buttons. Here's an example that demonstrates the usage
of different button types:

HI COLLEGE 02
COMPONENTS
JTextField
`JTextField` is used to display and edit single-line text. Here's an example that
demonstrates the usage of `JTextField`

HI COLLEGE 03
COMPONENTS
JScrollPane
`JScrollPane` provides a scrollable view of a component. Here's an example that shows
how to use `JScrollPane`

HI COLLEGE 04
COMPONENTS
JList
`JList` displays a list of items. Here's an example that demonstrates the usage of `JList`

HI COLLEGE 05
COMPONENTS
JComboBox
`JComboBox` provides a drop-down list of items from which the user can select one.
Here's an example that shows how to use `JComboBox`

HI COLLEGE 06
CONTAINERS [IMP]
Containers in Swing are parts that can store and arrange other parts. They offer a way of
organising and arranging the user interface components inside a window or frame. You
can utilise one of the several container classes offered by Swing to create the user
interface for your application. Typical examples of containers include:

JFrame
`It is the top-level container that represents the main window of an application. It provides
a title bar, borders, and buttons for minimizing, maximizing, and closing the window.

JPanel
It is a general-purpose container that is often used as a building block for organizing
components within a larger container. It does not have a title bar or borders like JFrame.

JDialog
It is a dialog box that is typically used to display messages, prompts, or forms to the user. It
can be modal or non-modal, depending on whether it blocks input to other windows.

JScrollPane
It is a container that provides a scrollable view of its contents when the contents exceed
the visible area. It is often used to wrap components such as JTextArea, JList, or JPanel that
may have a larger size than the available space.

JTabbedPane
IIt is a container that allows you to switch between multiple tabs, where each tab can
contain its own set of components. It provides a convenient way to organize and display
different sections or views within a single window.

JPanel
IIt is a lightweight container that is commonly used to group related components
together. It allows you to lay out components using various layout managers.

HI COLLEGE 07
CONTAINERS

Here's an example that demonstrates the usage of JFrame and JPanel:

HI COLLEGE 08
LAYOUT MANAGERS
Layout managers control the positioning and sizing of components within a
container. They help you achieve a consistent and flexible layout for your GUI.
Swing provides various layout managers, each with its own approach to
arranging components. Here are some commonly used ones:

FlowLayout
Places components in a row, wrapping to the next line if necessary. It's simple and
suitable for simple layouts.

HI COLLEGE 09
LAYOUT MANAGERS

BorderLayout
Divides the container into five regions: NORTH, SOUTH, EAST, WEST, and CENTER.
Components can occupy one or more regions.

GridLayout
Arranges components in a grid of rows and columns, where each cell holds a component.

HI COLLEGE 10
LAYOUT MANAGERS

GridBagLayout
GridBagLayout is a flexible layout manager in Swing that allows you to create complex
and customizable layouts. It uses a grid of cells to organize components, where each
cell can have different sizes and constraints.

HI COLLEGE 11
EVENT DELAGATION MODEL

The event delegation model in Java Swing is a design pattern


that handles and dispatches events within a graphical user
interface (GUI) application. It follows the principle of
delegating event handling to appropriate components,
allowing multiple components to listen and respond to events.

We mainly have two listener events


1. Keyboard listener events
2. Mouse listener events

These have their further more events which we’ll discuss below.

EVENT HANDLING IN JAVA [MOST IMPORTANT]

Event Sources
Event sources are the components that generate events, such as buttons,
checkboxes, or text fields.
These components have built-in mechanisms to notify the event system when a
specific event occurs.
Examples of event sources in Swing include JButton, JCheckBox, and JTextField

Event Listeners:
Event listeners are objects that register to receive and handle specific types of
events.
They listen for events from event sources and respond accordingly.
Event listeners must implement appropriate listener interfaces provided by Swing
to handle specific event types.
Commonly used listener interfaces include ActionListener, MouseListener, and
KeyListener.

HI COLLEGE 12
EVENT DELAGATION MODEL

Event Classes and Interfaces


Swing provides a set of event classes and interfaces that define various types of
events and their associated data.
These classes and interfaces serve as a contract between event sources and event
listeners.
For example, the ActionEvent class represents an action event triggered by
components like buttons, and the ActionListener interface defines methods to
handle such events.

Adapter Classes
Adapter classes are convenience classes that provide default implementations for
listener interfaces.
They allow you to implement only the methods you need, instead of implementing
all the methods in an interface.
Adapter classes have names ending with "Adapter."
For example, the MouseAdapter class provides default empty implementations for
all the methods in the MouseListener interface, allowing you to extend the
MouseAdapter and override only the methods you require.

HI COLLEGE 13
HOW YOU SHOULD WRITE ANSWERS -
Mouse Listener Events
MouseClicked: Triggered when the mouse button is pressed and
released quickly.
MousePressed: Fired when a mouse button is pressed down.
MouseReleased: Occurs when a mouse button is released.
MouseEntered: Fired when the mouse enters a component.
MouseExited: Triggered when the mouse exits a component.
MouseDragged: Fired when the mouse is dragged (moved while
holding a button down).
MouseMoved: Occurs when the mouse is moved without any buttons
being held down.

HI COLLEGE 14
Key Listener Events:
1. KeyTyped: Fired when a key is typed (pressed and released) on the
keyboard.
2. KeyPressed: Triggered when a key is initially pressed down.
3. KeyReleased: Occurs when a key is released after being pressed.

HI COLLEGE 15

You might also like