JAVA Unit 4
JAVA Unit 4
abstract window
toolkit(AWT) and
swing components
V2V RAJAN
CLASSES
SHUKLA
Professor
V2V Classes
Java and GUI support
import java.applet.Applet;
import java.awt.Graphics;
Definition:
1. AWT controls are pre-defined classes in the java.awt package that
enable the creation of interactive graphical user interfaces (GUIs).
Purpose:
2. They serve as basic building blocks for user interaction, such as
entering data, making selections, and triggering events.
Class Button
Definition: Allows single-line text input, often used for data entry
or searching.
Important Methods:
setText(String text): Sets the text in the field.
getText(): Retrieves the entered text.
setEditable(boolean editable): Enables or disables
editing. Constructor:
TextField(): Creates an empty text field.
TextField(int columns): Creates a text field with specified
width.
Class TextArea
Definition: A multi-line editable text area for longer text input. Important
Methods:
append(String str): Appends text to the text area.
setText(String text): Sets the content of the text area.
getText(): Retrieves the current content.
Constructor:
TextArea(): Creates an empty text area.
TextArea(int rows, int columns): Creates a text area with
specified dimensions.
Class Panel
Region Constants:
Definition: A flexible grid-based layout where rows and columns can have
variable sizes, and components can span multiple rows or columns.
Important Methods:
setConstraints(Component comp, GridBagConstraints
gbc): Sets constraints for a component.
getLayoutAlignmentX(Container parent): Gets the alignment
along the x-axis.
getLayoutAlignmentY(Container parent): Gets the alignment
along the y-axis.
Constructors:
GridBagLayout(): Creates a grid bag layout with default constraints.
GridBagLayout
Menus in AWT
Menus in Java AWT are created using the classes Menu,
MenuBar, and MenuItem. Below is the detailed explanation of their
constructors and methods:
Class MenuBar
Description: The MenuBar class is used to create a menu bar that holds
menus. A menu bar can be added to a frame using the setMenuBar()
method.
Constructors:
1. MenuBar(): Creates an empty menu bar.
Methods:
1. add(Menu m): Adds a menu to the menu bar.
2. remove(int index): Removes the menu at the specified index.
3. remove(Menu m): Removes the specified menu.
4. getMenu(int index): Returns the menu at the specified index.
5. getMenuCount(): Returns the number of menus in the menu bar.
6. setHelpMenu(Menu m): Sets the help menu (usually the last menu on the
right).
Class Menu
Description: The Menu class represents a pull-down menu that contains menu
items.
Constructors:
1. Menu(): Creates an empty menu with no label.
2. Menu(String label): Creates a menu with the specified label.
3. Menu(String label, boolean tearOff): Creates a menu with the specified label
and a tear-off functionality.
Methods of Menu :
Constructors:
1. FileDialog(Frame parent):Creates a file dialog with the specified parent window
(frame) for selecting files.
2. FileDialog(Frame parent, String title):Creates a file dialog with the
specified parent window and title for the dialog box.
3. FileDialog(Frame parent, String title, int mode):Creates a file dialog with
the specified parent window, title, and mode (either FileDialog.LOAD or
FileDialog.SAVE).
FileDialog.LOAD: For loading a file.
FileDialog.SAVE: For saving a file.
Important Methods:
● Platform Dependency
● Limited Customizability
● Lack of Advanced GUI Components
● Poor Layout Management
● Inconsistent Event Handling
● Limited Look and Feel
● No Support for Modern GUI Features
● Limited Multi-threading Support
● Lack of Pluggable Look and Feel
● Limited Support for Event Dispatching
Features of Swing Components
● Lightweight Components
● Pluggable Look and Feel
● Rich Set of GUI Components
● Model-View-Controller (MVC) Architecture
● Improved Event Handling
● Customizable Components
● Double Buffering
● Platform Independence
● Better Layout Management
● Support for Advanced GUI Features
Difference Between AWT and Swing Components
Swing components classes
Swing components are the building blocks for creating rich and interactive
graphical user interfaces in Java. They provide a more flexible and customizable
approach to building UIs compared to AWT components.
Definition:
Swing components are lightweight, meaning they are drawn entirely by Java rather
than relying on native operating system components. Swing allows developers to
create highly interactive, visually appealing, and platform- independent GUI
applications. It supports a wide variety of components and layouts, with better
control over the look and feel of the application.
class JComponent
Definition:
The base class for all Swing components, providing common properties and methods.
Constructors:
JComponent(): Initializes a new JComponent object.
Methods:
setVisible(boolean aFlag): Sets the visibility of the component.
setSize(int width, int height): Sets the size of the component.
setLayout(LayoutManager manager): Sets the layout manager for the
component.
repaint(): Requests that the component be repainted.
setBackground(Color bg): Sets the background color of the component.
Constants:
UIResource: A marker interface indicating that a component's UI delegate is an
UIResource.
class JFrame
Definition:
A top-level container used to create a window for Swing applications.
Constructors:
JFrame(): Creates a new JFrame object with a default title.
JFrame(String title): Creates a new JFrame object with the specified title.
Methods:
setVisible(boolean b): Makes the frame visible or invisible.
setSize(int width, int height): Sets the size of the frame.
setTitle(String title): Sets the title of the frame.
setDefaultCloseOperation(int operation): Sets the operation that occurs
when the user closes the frame.
setLayout(LayoutManager layout): Sets the layout manager for the frame.
Constants:
JFrame.EXIT_ON_CLOSE: Specifies that the application should
exit when the user closes the window.
JFrame.HIDE_ON_CLOSE: Specifies that the frame should be
hidden when the user closes the window.
class JApplet
Definition:
Swing-based applet used for applet-based applications (though
applets are outdated).
Constructors:
JApplet(): Creates a new JApplet object.
Methods:
init(): Initializes the applet (called once during the applet's lifecycle).
start(): Starts the applet (called when the applet is visible).
stop(): Stops the applet (called when the applet is no longer visible).
destroy(): Destroys the applet (called when the applet is unloaded).
Constructors:
JToolTip(): Creates a new JToolTip object.
Methods:
setTipText(String tipText): Sets the text that appears in the tooltip.
getTipText(): Returns the text in the tooltip. setComponent(Component
component): Sets the component that the tooltip is associated with.
Importance of Event Handling
Constructors
1. ItemEvent(ItemSelectable source, int id, Object item, int stateChange)
2. Creates an ItemEvent for the specified source, ID, item, and state change.
Methods
1. getItem(): Returns the affected item.
2. getItemSelectable(): Returns the component that generated the event.
3. getStateChange(): Returns the state change (selected or deselected).
Constants
SELECTED: Indicates the item was selected.
DESELECTED: Indicates the item was deselected.
Integer constant : ITEM_STATE_CHANGED
KeyEvent
Constants
KEY_FIRST: The first integer ID for key events.
KEY_LAST: The last integer ID for key events.
VK_A - VK_Z: Key codes for alphabet keys.
MouseEvent
Constructors
1. MouseEvent(Component source, int id, long when, int modifiers, int x, int y, int
clickCount, boolean popupTrigger)
2. Creates a MouseEvent with specified coordinates, click count, and popup
trigger status.
TextEvent
Constructors
1. TextEvent(Object source, int id)
2. Creates a TextEvent with the specified source and ID.
Methods
1. getSource()
2. Returns the object that generated the event.
Constants
TEXT_FIRST: The first integer ID for text events.
TEXT_LAST: The last integer ID for text events.
Integer constant : TEXT_VALUE_CHANGED
WindowEvent
Types of Window Events in WindowEvent Class
ItemListener
ItemListener
Method:
void itemStateChanged(ItemEvent e): Triggered when an item's state
changes (e.g., checkbox selection).
KeyListener
Methods:
void keyPressed(KeyEvent e): Triggered when a key is pressed. void
keyReleased(KeyEvent e): Triggered when a key is released.
void keyTyped(KeyEvent e): Triggered when a key is pressed and released,
generating a character.
MouseListener
Methods:
void mouseClicked(MouseEvent e): Triggered when a mouse button is
clicked.
void mousePressed(MouseEvent e): Triggered when a mouse button is
pressed.
void mouseReleased(MouseEvent e): Triggered when a mouse button is
released.
void mouseEntered(MouseEvent e): Triggered when the mouse enters a
component.
void mouseExited(MouseEvent e): Triggered when the mouse exitsa
component.
MouseMotionListener
Methods:
void mouseMoved(MouseEvent e): Triggered when the mouse is moved. void
mouseDragged(MouseEvent e): Triggered when the mouse is moved while a
button is pressed.
TextListener
Method:
void textValueChanged(TextEvent e): Triggered when the
value of a text component changes.
Thankyou :)