0% found this document useful (0 votes)
20 views16 pages

Java - 1 Meet Ref

This is javaa program

Uploaded by

nandish4647
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)
20 views16 pages

Java - 1 Meet Ref

This is javaa program

Uploaded by

nandish4647
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/ 16

UNIT – 1 JAVA SWING

INTRODUCTION TO JFC & SWING WITH FEATURES


WHAT IS JFC ?
JFC (Java Foundation Classes) is a set of application programming interfaces (APls) that provide a
framework for building graphical user interface (GUI) applications in Java. Swing is a part of JFC and
serves as a replacement for AWT (Abstract Window Toolkit), providing more powerful and flexible
GUI components.

FEATURES :
1) Lightweight Components : Swing components are lighter and don’t depend on the system’s native
window components. This makes them more flexible and faster.

2) More Controls : Swing has a larger set of built-in tools, like trees, image buttons, sliders, and more,
compared to AWT.

3) Customizable : You can change the look of Swing components more easily. You can adjust their
appearance, such as borders and text alignment, and even add images.

4) Pluggable Look and Feel : Swing allows you to change the look of your app at runtime or create
your own design style.

5) Extra Features : Swing includes features like smooth graphics (double-buffering), tool tips, and
dockable toolbars that are not available in AWT.

6) Platform Independence : Swing works the same on any platform that supports Java, so your app
will look and behave consistently across different systems.

7) Rich Components : Swing provides a wide range of GUI components, like buttons, labels, text
fields, and more, to suit various needs.

8) Advanced Features : Swing supports drag-and-drop, undo/redo, accessibility, and


internationalization.

9) Flexible Layouts : Swing offers various layout managers to arrange components in a flexible and
responsive way.

10) Better Event Handling : Swing has robust methods for handling user interactions like clicks and
key presses.

11) MVC Architecture : Swing supports the Model-View-Controller (MVC) design, helping to keep
your code organized by separating data, appearance, and logic.

12) Graphics & 2D APIs : Swing includes tools for creating custom graphics and animations.
13) Built-in Dialogs : Swing has ready-made dialogs for common tasks like file selection and color
picking.

14) Works with AWT : Swing can be used alongside AWT components in the same application, if
needed.
Difference between AWT and Swing :
The main difference between AWT and Swing is that AWT is a platform-dependent GUI library that
relies on the underlying operating system's components, while Swing is a platform-independent
library that provides its own set of GUl components. Swing components have a consistent look and
feel across all platforms, which is not guaranteed in AWT.

AWT (Abstract Window Toolkit) Swing

Platform-dependent Platform-independent

Uses native OS components Provides its own set of components

Limited look and feel control Customizable look and feel across platforms

Heavier in terms of resources Lightweight and efficient

Less consistent across platforms Consistent look and feel across platforms

Advantages of Using Swing Over AWT :


1) Consistent Look :
- Swing makes sure your app looks the same on all operating systems, unlike AWT, which can look
different depending on where it’s run.

2) More Features :
- Swing has more and better features for creating user interfaces, like tables and tabs, which AWT
doesn’t have.

3) Flexibility :
Swing components are more flexible and can be customized more easily because they don’t depend
on the operating system.

4) Custom Looks :
- Swing allows you to change the appearance of components or use different styles, which helps in
making your app look exactly how you want it to.

5) Better Event Handling :


- Swing has a more advanced system for dealing with user actions, making it easier to create
responsive and interactive apps.

6) Advanced Graphics :
- Swing offers better options for drawing and designing, letting you create more visually appealing
and unique user interfaces.
SWING CLASS HIERARCHY :
At the root of the Swing hierarchy is the java.awt.Component class, which is part of the AWT library.
Swing components extend the Component class to provide more specialized and enhanced
functionality.

1) Object
- The root class of the entire Java class hierarchy. Every class in Java, including all AWT and Swing
classes, ultimately inherits from Object.

2) Component (java.awt.Component)
- The base class for all AWT and Swing components. It provides the common functionality needed for
any visual element, such as buttons, text fields, and labels.
3) Container (java.awt.Container)
- A subclass of Component that can hold other components, allowing for the creation of complex user
interfaces.
(I) Window (java.awt.Window)
- A top-level container that represents a standalone window. It does not have borders or a menu
bar.
(i) Frame (java.awt.Frame)
- A top-level window with a title and a border. It can hold other components and is
often used as the main window for an application.
(ii) Dialog (java.awt.Dialog)
- A pop-up window used to gather user input or display messages. It is often used for
alerts, confirmations, or additional input forms.

(II) Panel (java.awt.Panel)


- A generic, lightweight container used to organize other components. It serves as a basic
container for grouping components together.
(i)Applet (java.applet.Applet)
- A panel that can be embedded within a web page and run inside a web browser or
applet viewer. It extends the Panel class.

4) JComponent (javax.swing.JComponent)
- Extends Container and serves as the base class for all Swing components. It provides advanced
functionality such as double buffering, painting, and pluggable look-and-feel, which enhances the
capabilities of Swing components beyond those provided by AWT.
Discuss purpose & usage of commonly used methods in component class :

1) public void add(Component c) :


- This method is used to add a child component to a container. Component is the base class. The
Component class is the base class for all GUI elements, so you use this method to place various
components within a container.

2) public void setSize(int width, int height) :


- This method sets the size of the component by taking height and width as parameters.

3) public void setLayout(LayoutManager manager) :


- This method sets the layout to set the components in a particular manner by taking LayoutManager
as a parameter.

4) public void setVisible(boolean status) :


- This method controls the visibility of the component. If status is true, the component is made visible;
if false, the component is hidden.
Discuss the purpose & usage of swing components :
1) JFrame :
- JFrame represents a top-level window with a title, border, and the ability to contain other
components. It is the main window of a Swing application.
- Usage : To create the main application window.

2) JLabel :
- JLabel is used to display a short string or an image icon. It is a non-editable text component,
meaning user can not modify its content directly.
- Usage : To show static text or images in a GUI.

3) JTextField :
- It allows users to input a single line of text where user can enter and edit text.
- Usage : For user input, such as entering names or search queries.

4) JButton :
- It is a component that trigger an action when clicked by that user.
- Usage : To create buttons for user interactions.

5) JTextArea :
- It is a multi line text component that allows users to enter and edit large amount of text.
- Usage : For longer text input or displaying multi-line text.

6) JCheckBox :
- It is used to create a toggleable option that can be checked or unchecked, allowing users to select or
deselect options.
- Usage : Use it for options that can be toggled on or off.

7) JRadioButton :
- It allows the user to choose one option from a group of options.
- Usage : It is used in groups, typically managed by a “ButtonGroup”, where only one button can be
selected at a time.

8) JComboBox :
- It is a drop down list that allows the user to select one item from a list of options. It is used when you
want to present a list of choices to the users, but only one choice can be selected at a time.
- Usage : For selecting an option from a predefined list.

9) JMenu :
- It represents a menu in a menu bar, which contain menu items. It is used to create pull-down in
applications, such as “File” , “Edit” or “Help” menu, where users can choose different commands.
- Usage : Use JMenu as part of a JMenuBar to provide a menu in an application.
Describe the following layout managers :
Layout Management in Swing refers to how components are arranged within containers like JFrames
or JPanels. Different layout managers allow you to control the positioning and resizing of
components.

List of commonly used layout managers :


1. FlowLayout : Components are arranged in a row, and if the row fills up, the next component wraps
to the next row.

2. BorderLayout : Components are organized in five regions: North, South, East, West, and Center.

3. CardLayout : Components are stacked on top of each other, and you can switch between them like
cards.

4. BoxLayout : Components are arranged in a single line (either horizontally or vertically).

5. GridLayout : Components are arranged in a grid with rows and columns.

6. GridBagLayout : Allows fine-grained control over the placement of components using constraints.

7. GroupLayout : Allows flexible, auto-generated layouts based on specified groups of components.

8. SpringLayout : A flexible layout manager that uses spring constraints to define component
positions.
1. FLOWLAYOUT :
- FlowLayout is a simple layout manager that arranges components in a row or a column. Components
are added from left to right in a row (or top to bottom in a column). When a row or column is filled, it
wraps to the next row or column.

Syntax :
FlowLayout flowLayout = new FlowLayout();
FlowLayout flowLayout = new FlowLayout(align,hgap,vgap);

- align: Specifies the alignment of components within the row/column.


- hgap : Horizontal gap between component.
- vgap : Vertical gap between rows or columns.

2. BORDERLAYOUT :
BorderLayout is a layout manager that divides the container into five regions: North, South, East,
West, and Center. Each region can hold only one component, and the components are resized to fit the
assigned region.

Syntax :
BorderLayout borderLayout = new BorderLayout();

- hgap : Horizontal gap between component.


- vgap : Vertical gap between rows or columns.

3. CARDLAYOUT :
CardLayout is a layout manager that arranges components in a stacks, like a deck of cards, where only
one component(deck) is visible at a time. Only one component is visible at a time, and you can switch
between them.

Syntax :
CardLayout cardLayout = new CardLayout();

4. BOXLAYOUT :
BoxLayout is a layout manager that arranges components in a single line either horizontally or
vertically. It allows you to create flexible layouts easily.

Syntax :
BoxLayout boxLayout = new BoxLayout(container, axis);

- Container : The container that holds the components.


- axis : The axis along which components are aligned
- BoxLayout.X_AXIS: Arranges components horizontally.
- BoxLayout.Y_AXIS: Arranges components vertically.
5. GRIDLAYOUT :
GridLayout is a layout manager that arranges components in a grid format with specified number of
rows and columns. All components has an equal amount of space within the grid.

Syntax :
GridLayout gridLayout = new GridLayout(rows, cols);

- rows : The number of rows in the grid.


- cols : The number of columns in the grid.

6. GRIDBAGLAYOUT :
GridBagLayout is a powerful layout manager that arranges components in a grid-like structure,
providing control over their positioning and sizing.

Syntax :
GridBagLayout gridBagLayout = new GridBagLayout();

7. GROUPLAYOUT :
GroupLayout is a layout manager that arranges components in groups, making it especially suited for
use with GUI builders. It offers a flexible way to position and align components based on their logical
grouping.

Syntax :
GroupLayout groupLayout = new GroupLayout(container);
container.setLayout(groupLayout);

8. SPRINGLAYOUT :
SpringLayout is a flexible layout manager that uses spring constraints to define the positioning and
sizing of components relative to each other.

Syntax :
SpringLayout springLayout = new SpringLayout();
Describe the concept of Event Handling in Java :
What is Event Handling :
Event Deligation Model :

- Event handling in Swing involves responding to user interactions like button clicks, key presses,
mouse movements, etc. When an event occurs, an event object is triggered, and you can write event
listeners to handle these events. This allows users to respond to various interactions. Event handling
process :
1) Event Source :
- An event source is a component that generates an event, such as buttons, text fields, checkboxes, and
other interactive components.

2) Event Object :
- When an event occurs, an event object is created. This object contains information about the event,
such as its type, the source of the event, and additional details.

3) Event Listener :
- An event listener is an interface that defines one or more methods to handle events. When an event
occurs, the corresponding method in the event listener is called.

4) Event Registration :
- To handle events, an event listener must be registered with the event source. This is done by calling
an addListener method on the component.

This process allows the program to capture events and execute specific code in response to them,
enabling a dynamic and interactive user interface in Swing applications.

Component Types :
1. Action Events : Triggered by components like buttons when they are activated (e.g., clicked).

2. Key Events : Triggered when the user interacts with the keyboard (e.g., pressing a key).

3. Focus Events : Triggered when a component gains or loses focus.

4. Window Events : Triggered when window-related events occur (e.g., closing the window).

5. Mouse Events : Triggered by mouse actions (e.g., clicking, moving).

6. Item Events : Triggered by components like checkboxes, radio buttons when their state
1. ACTION EVENTS :
Action Events occur when a user interacts with components that can be activated, like buttons or
menu items, by clicking or pressing the Enter key. These events are used to respond to user actions
and trigger specific actions in the application.
- Usage : Used for buttons and menu items. Example: Clicking a "Submit" button to send a form.

Syntax :

2. KEY EVENTS :
Key Events are triggered when the user interacts with the keyboard. They are used to capture
keystrokes and perform actions based on those keystrokes.
- Usage : Used for keyboard actions. Example: Pressing Enter to start a search.

Syntax :
3. FOCUS EVENTS :
Focus Events are triggered when a component gains or loses keyboard focus. They are used to
perform actions when a component becomes active or inactive.
- Usage : Used when a component gains or loses focus. Example: Saving changes when leaving a text
field.

Syntax :

4. ITEM EVENTS :
Item Events are triggered when the selection of an item in a component changes, such as selecting an
item in a combo box or checking/unchecking a checkbox.
- Usage : Used when selecting or changing items in checkboxes or combo boxes. Example: Updating
options based on a dropdown selection.

Syntax :
5. MOUSE EVENT :
Mouse Events are triggered when the user interacts with the mouse, such as clicking, pressing,
releasing, or moving it.
- Usage : Used for mouse actions like clicks and movements. Example: Dragging an item or clicking a
button.

Syntax :
6. WINDOW EVENT :
Window Events are triggered when the state of a window changes, such as opening, closing,
activating, or deactivating a window.
- Usage : Used for changes in window state. Example: Adjusting the layout when resizing a window
or saving data when closing.

Syntax :
EVENTLISTENER INTERFACE :
The EventListener interface is the basic interface for all event listener interfaces in Swing. It doesn’t
have methods but serves as a base for other event-specific listener interfaces.

Commonly used event listener interfaces:


1. ActionListener: Handles action events (e.g., button clicks).
2. KeyListener: Handles key events.
3. FocusListener: Handles focus events.
4. WindowListener: Handles window-related events.
5. MouseListener: Handles mouse events.
6. MouseMotionListener: Handles mouse motion events.
7. ItemListener: Handles item events (e.g., checkbox/radio button state changes).
1. ACTIONLISTENER :
- It is an interface used to handle action like button clicks or menu selection. When you click a button
or select a menu item, it triggers an ActionEvent, and the actionPerformed() method is called to
handle it.
- Purpose : Handles actions like button clicks or menu selections.
- Usage : Implement this to execute code when a user interacts with buttons or menu items.

2. KEYLISTENER :
- It is an interface used to handle keyboard events. It allows you to capture and respond to keyboard
events like key presses, releases, and typing.
- Purpose : Handles keyboard events such as key presses and releases.
- Usage : Implement this to respond to specific keys being pressed or released.

3. FOCUSLISTENER :
- It is an interface used to handle focus events. It allows you to perform actions when a component
like a text field, becomes active or inactive.
- Purpose : Handles when a component gains or loses focus.
- Usage : Implement this to manage what happens when a user starts or stops interacting with a
component, like highlighting a text field when selected.

4. WINDOWLISTENER :
- It is an interface used to handle window events, such as opening, closing, activating, and
deactivating a window.
- Purpose : Handles changes in window state like opening, closing, or resizing.
- Usage : Implement this to control actions when a window is opened, closed, or resized, such as
saving data before closing.

5. MOUSELISTENER :
- It is an interface used to handle mouseactions. It allows you to capture and respond to mouse
actions, such as clicking, pressing, releasing, entering, and exiting components.
- Purpose : Handles mouse events like clicks, presses, and releases.
- Usage : Implement this to respond to mouse actions, such as showing a menu on right-click or
changing a button’s appearance when hovered over.

6. MOUSEMOTIONLISTENER :
- It is an interface used to handle mouse motion events. It allows you to capture and respond to mouse
movement.
- Purpose : Handles mouse movement events like dragging or moving the mouse over a component.
- Usage : Implement this to track mouse movement, useful for drag-and-drop or drawing applications.

7. ITEMLISTENER :
- It is an interface used to handle changes in itemselection. It is commonly used with components that
allow item selection, such as checkboxes and combo boxes.
- Purpose : Handles changes in item selection, like checking a checkbox or selecting a dropdown item.
- Usage : Implement this to respond to changes in selectable items, such as toggling options in a form
or handling dropdown menu selections.

You might also like