Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Department Of Computer Science

Course Title: Advanced Programming

Target Group: Computer Science 2nd year.

Instructor Name: Abebe Z.

12/11/2021 1
Awt and Swing
Introduction to Swing Programming
Swing provides classes representing interface items like windows,
buttons, combo boxes, trees, grids, and menus-everything you need
to build a user interface for your Java application. The javax.swing
package (and its numerous sub packages) contains the swing user
interface classes.
Swing is part of a larger collection of software called the Java
Foundation Classes (JFC). JFC includes the following APIs:

 The Abstract Window Toolkit (AWT), the original user interface toolkit.
 Swing, the new user interface toolkit .
 Drag and Drop, an API that supports the drag-and-drop metaphor

12/11/2021 2
Continued….
• To understand Swing, it better to understand its predecessor, the
Abstract Window Toolkit (AWT). As its name suggests, AWT is an
abstraction. Its classes and functionality are the same for all Java
implementations, so Java applications built with AWT should work
in the same way on all platforms. You could choose to write your
code under Windows, and then run it on an X Window System or a
Macintosh. To achieve platform dependence, AWT uses
interchangeable toolkits that interact with the host windowing
system to display user interface components. These shield your
application from the details of the environment it's running in. Let's
say you ask
AWT to create a button. When your application or applet runs, a
toolkit appropriate to the host environment renders the button
appropriately: on Windows, you can get a button that looks like
other Windows buttons; on a Macintosh, you can get a Mac button;
and so on.

12/11/2021 3
Continued….
AWT had some serious shortcomings. The worst was that
the use of platform-specific toolkits meant that AWT
applications might be subtly incompatible on different
platforms. Furthermore, AWT was lacking advanced user
interface components, like trees and grids. Swing takes a
fundamentally different approach. Instead of using native
toolkits to supply interface items like buttons and combo
boxes, components in Swing are implemented in Java
itself. This means that, whatever platform you're using, a
Swing button (for example) looks the same. It also makes
Swing much less prone to platform-specific bugs, which
were a problem for AWT.

12/11/2021 4
Continued….
• Swing uses layout managers to arrange components
inside containers and control their sizing and
positioning. Layout managers define a strategy for
arranging components instead of specifying absolute
positions. For example, you can define a user interface
with a collection of buttons and text areas and be
reasonably confident that it will always display correctly,
even if the user resizes the application window. It
doesn't matter what platform or user interface look and
feel you're using; the layout manager should still
position them sensibly with respect to each
other.

12/11/2021 5
Hierarchy of Java Swing classes

12/11/2021 6
Java AWT Hierarchy

12/11/2021 7
Difference between AWT and Swing
Here are the differences between java Awt and swing that are given
below.
Java Awt Java Swing

 AWT components are platform-  Java swing components are platform-


dependent. independent

 AWT doesn't support pluggable look  Swing supports pluggable look and feel.
and feel.

 AWT provides less components than  Swing provides more powerful


Swing. components such as tables, lists, scroll
panes, color chooser, tabbed pane etc

12/11/2021 8
Component
• A component is the fundamental user interface object in Java.
Everything you see on the display in a Java application is a
component. This includes things like windows, drawing
canvases, buttons, checkboxes, scrollbars, lists, menus, and
text fields. To be used, a component usually must be placed in a
container. Container objects group components, arrange them
for display using a layout manager, and associate them with a
particular display device. All Swing components are derived
from the abstract javax.swing.JComponent class. The previous
figure shows the user interfaces in the javax.swing package. For
example, the Jbutton class is a subclass of Abstract Button,
which is itself a subclass of the JComponent class.

12/11/2021 9
Continued….

• Because JComponent inherits from Container, it has


the capabilities of both a component
and a container. AWT and Swing, then, have parallel
hierarchies. The root of AWT's hierarchy is
Component, while Swing's components are based on
JComponent. You'll find similar classes
in both hierarchies, like Button and JButton. But
Swing is much more than simply a replacement for
AWT—it contains sophisticated components, like
trees and grids.

12/11/2021 10
Container
• A container is a kind of component that holds and manages
other components. JComponent objects can be containers,
because the JComponent class descends from the Container
class. Three of the most useful container types are JFrame ,
JPanel, and JApplet. A JFrame is a top-level window on your
display. JFrame is derived from JWindow, which is pretty
much the same but lacks a border. A JPanel is a generic
container element used to group components inside of
JFrames and other JPanels. The Japplet class is a kind of
container that provides the foundation for applets that run
inside web browsers. Like every other JComponent, a
JApplet has the ability to contain other user interface
components .

12/11/2021 11
Layout Managers
• A layout manager arranges the child components of a container.
It positions and sets the size of components within the
container's display area according to a particular layout scheme.
The layout manager's job is to fit the components into the
available area while maintaining the proper spatial relationships
among the components. Every container has a default layout
manager; therefore, when you make a new container, it
comes with a LayoutManager object of the appropriate type. You
can install a new layout manager at any time with the
setLayout() method. For example, we can set the layout manager
of a container to a BorderLayout:
setLayout (new BorderLayout());

12/11/2021 12
Continued…..
• Swing provides reasonable types of layout managers. Let us see
them one by one.
• FlowLayout:
FlowLayout is a simple layout manager that tries to arrange
components with their preferred sizes, from left to right and top
to bottom in the container. A
FlowLayout can have a specified row justification of LEFT,
CENTER, or RIGHT, and a fixed horizontal and vertical
padding. By default, a flow layout uses CENTER justification,
meaning that all components are centered within the area
allotted to them. FlowLayout is
the default for JPanels.

12/11/2021 13
Continued….
• import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Flow extends JFrame{
public static void main(String args[]){
JFrame f = new JFrame("LayoutManager: FlowLayout");
f.addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) { System.exit(0); }
});
f.setLayout(new FlowLayout());
f.add(new JButton("One"));
f.add(new JButton("Two"));
f.add(new JButton("Three"));
f.add(new JButton("Four"));
f.add(new JButton("Five"));
f.setSize(400,200);
f.setLocation(200,200);
f.setVisible(true);
}
}

12/11/2021 14
Continued…..
• Grid Layout:
• Grid Layout arranges components into regularly spaced rows and
columns. The components are arbitrarily resized to fit the grid; their
minimum and preferred sizes are consequently ignored. Grid Layout
is most useful for arranging identically sized objects— perhaps a set
of JPanels, each using a different layout manager. Grid Layout takes
the number of rows and columns in its constructor. If you
subsequently give it too many objects to manage, it adds extra
columns to make the objects fit. You can also set the number of rows
or columns to zero, which means that you don't care how many
elements the layout manager packs in that dimension. For example,
Grid Layout(2,0) requests a layout with two rows and an unlimited
number of columns; if you put ten components into this layout,
you'll get two rows of five columns each.

12/11/2021 15
Continued…..
• import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Grid extends JFrame{
public static void main(String args[]){
JFrame f = new JFrame("LayoutManager: GridLayout");
f.addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) { System.exit(0); }
});
f.setLayout(new GridLayout(2,3)); //The output is
f.add(new JButton("One"));
f.add(new JButton("Two"));
f.add(new JButton("Three"));
f.add(new JButton("Four"));
f.add(new JButton("Five"));
f.setSize(400,150);
f.setLocation(200,200);
f.setVisible(true);
}
}

12/11/2021 16
Continued…..
• Border Layout:
• BorderLayout is a little more interesting. It tries to arrange
objects in one of five geographical locations, represented by
constants in the BorderLayout class: NORTH, SOUTH, EAST,
WEST, and CENTER, possibly with some padding between.
BorderLayout is the default layout for the content panes of
JWindow and JFrame objects. Because each component is
associated with a direction, BorderLayout can manage at most
five components; it squashes or stretches those components to
fit its constraints. When we add a component to a border layout,
we need to specify both the component and the position at
which to add it. To do so, we use an overloaded version of the
add() method that takes an additional argument as a constraint

12/11/2021 17
Continued…..
• import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Border extends JFrame{
public static void main(String args[]){
JFrame f = new JFrame("LayoutManager: BorderLayout");
f.addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) { System.exit(0); }
});
f.setLayout(new BorderLayout());
f.add(new JButton("One"), BorderLayout.WEST);
f.add(new JButton("Two"), BorderLayout.NORTH);
f.add(new JButton("Three"), BorderLayout.EAST);
f.add(new JButton("Four"), BorderLayout.SOUTH);
f.add(new JButton("Five"), BorderLayout.CENTER);
f.setSize(400,250);
f.setLocation(200,200);
f.setVisible(true);
}
}

12/11/2021 18

You might also like