UNIT-5 JAVA
1. Introduction to AWT and Swing:
- AWT (Abstract Window Toolkit):
- AWT is the foundational GUI library in Java.
- It provides a set of classes for creating and managing graphical user
interfaces.
- Key AWT components include `Frame`, `Button`, `Label`, and `TextField`.
- AWT components are heavyweight, meaning they rely on the native
platform's GUI components.
- Swing:
- Swing is built on top of AWT and provides a more advanced and flexible set
of GUI components.
- All Swing components are lightweight, meaning they are written entirely in
Java and do not depend on the native platform.
- Key Swing components include `JFrame`, `JButton`, `JLabel`, and
`JTextField`.
2. Differences between AWT and Swing:
S.NO
AWT Swing
Swing is a part of Java Foundation
Java AWT is an API to develop
1. Classes and is used to create various
GUI applications in Java
applications.
The components of Java AWT are The components of Java Swing are
2.
heavy weighted. light weighted.
S.NO
AWT Swing
Java AWT has comparatively less
Java Swing has more functionality as
3. functionality as compared to
compared to AWT.
Swing.
The execution time of AWT is The execution time of Swing is less
4.
more than Swing. than AWT.
The components of Java AWT are The components of Java Swing are
5.
platform dependent. platform independent.
MVC pattern is not supported by
6. MVC pattern is supported by Swing.
AWT.
AWT provides comparatively less Swing provides more powerful
7.
powerful components. components.
AWT components require java.awt Swing components requires
8
package javax.swing package
AWT is a thin layer of code on top Swing is much larger swing also has
9
of the operating system. very much richer functionality.
Swing is also called as JFC(java
AWT stands for Abstract windows
10 Foundation classes). It is part of
toolkit .
oracle’s JFC.
Using AWT , you have to
11 implement a lot of things Swing has them built in.
yourself .
3. Creating Windows, Frames, Panels, and Forms:
- Creating a Frame (AWT):
- The `Frame` class represents the top-level window in AWT.
- Example:
```java
Frame frame = new Frame("My AWT Frame");
frame.setSize(300, 200);
frame.setVisible(true);
```
- Creating a JFrame (Swing):
- The `JFrame` class is used to create a top-level container in Swing.
- Example:
```java
JFrame frame = new JFrame("My Swing Frame");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
```
- Creating a Panel:
- Both AWT and Swing use panels as containers for organizing components.
- Example:
```java
Panel panel = new Panel();
frame.add(panel);
```
4. Working with UI Components:
- Command Buttons (Button and JButton):
- AWT's `Button` and Swing's `JButton` are used for clickable buttons.
- Example:
```java
Button awtButton = new Button("AWT Button");
JButton swingButton = new JButton("Swing Button");
```
- Text Fields (TextField and JTextField):
- AWT's `TextField` and Swing's `JTextField` are used for single-line text input.
- Example:
```java
TextField awtTextField = new TextField("AWT TextField");
JTextField swingTextField = new JTextField("Swing TextField");
```
- Labels (Label and JLabel):
- AWT's `Label` and Swing's `JLabel` are used for displaying text labels.
- Example:
```java
Label awtLabel = new Label("AWT Label");
JLabel swingLabel = new JLabel("Swing Label");
```
- List Boxes (List and JList):
- AWT's `List` and Swing's `JList` are used for displaying lists of items.
- Example:
```java
List awtList = new List();
JList<String> swingList = new JList<>(new String[]{"Item 1", "Item 2"});
```
5. Layout Manager:
- AWT Layout Managers:
- AWT provides layout managers like `FlowLayout`, `BorderLayout`, and
`GridLayout`.
- Example:
```java
Frame frame = new Frame("AWT Frame");
frame.setLayout(new BorderLayout());
```
- Swing Layout Managers:
- Swing provides more advanced layout managers like `FlowLayout`,
`BorderLayout`, `GridLayout`, `BoxLayout`, and `GridBagLayout`.
- Example:
```java
JFrame frame = new JFrame("Swing Frame");
frame.setLayout(new BorderLayout());
```
6. Menus and Tabbed Panes:
- Menus (Menu and JMenu):
- Both AWT and Swing support menu creation for organizing commands.
- Example:
```java
MenuBar menuBar = new MenuBar();
Menu menu = new Menu("File");
MenuItem menuItem = new MenuItem("Open");
menu.add(menuItem);
menuBar.add(menu);
frame.setMenuBar(menuBar);
```
- Tabbed Panes (JTabbedPane):
- A tabbed pane allows organizing components into tabs.
- Example:
```java
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Tab 1", new JPanel());
tabbedPane.addTab("Tab 2", new JPanel());
frame.add(tabbedPane);
```
7. Event Handling:
- Event Handling Basics:
- Event handling is the process of responding to user interactions like button
clicks or mouse movements.
- Components generate events, and event listeners are registered
to handle those events.
- Delegation Model:
- In Java, event handling follows a delegation model where components
delegate the handling of events to registered listeners.
- Event Classes:
- Events are represented by classes like `ActionEvent`, `MouseEvent`, etc.
- Example:
```java
JButton button = new JButton("Click Me");
button.addActionListener(e -> System.out.println("Button clicked!"));
```
- Event Listener Interfaces:
- Interfaces like `ActionListener`, `MouseListener`, etc., define methods to
handle specific events.
- Example:
```java
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
```
- Adapter Classes:
- Adapter classes provide default implementations for event listener
interfaces.
- Useful when only a subset of methods need to be implemented.
- Example:
```java
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked!");
}
});
```