Java Unit 4
Java Unit 4
SEMESTER 4
UNIT - 4
HI COLLEGE
SYLLABUS
UNIT - 4
HI COLLEGE
COMPONENTS
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
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
These have their further more events which we’ll discuss below.
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
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