Chapter One: Debre Markos University Department of Computer Science

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

Debre Markos University

Department of Computer Science

Chapter One
AWT AND SWING

Advanced Programming(CoSc2084)

03/18/2021
AWT AND SWING
* GUI stands for Graphical User Interface, a term used not only
in Java but in all programming languages that support the
development of GUIs. .
* It is made up of graphical components (e.g., buttons, labels,
windows) through which the user can interact with the page or
application
* Abstract Windows Toolkit (AWT) is JAVA’s original platform
dependent windowing, graphics, and user-interface widget toolkit
preceding swing.
* Swing API is a set of extensible GUI Components to ease the
developer's life to create JAVA based Front End/GUI Applications.
* It is also part of Java Foundation Classes.
* It is built on top of AWT API and acts as a replacement of AWT API,
since it has almost every control corresponding to AWT controls.
AWT AND SWING
Swing Features
* Light Weight- Swing components are independent of native
Operating System's API as Swing API controls are rendered
mostly using pure JAVA code instead of underlying operating
system calls.
* Rich Control- Swing provides a rich set of advanced controls
like Tree, TabbedPane, slider, colorpicker, and table controls.
* Highly Customizable - Swing controls can be customized in a
very easy way as visual appearance is independent of internal
representation.
* Pluggable look-and-feel - SWING based GUI Application look
and feel can be changed at run-time, based on available
values.
AWT AND SWING

* Java Swing is a part of Java Foundation Classes (JFC) that


is used to create window-based applications

* Unlike AWT, Java Swing provides platform-independent and


lightweight components.

* The javax.swing package provides classes for java swing API


such as JButton, JTextField, JTextArea, JRadioButton,
JCheckbox, JMenu, JColorChooser etc.
AWT Swing
AWT components are called Swing components are called light weight
heavyweight components components because these components sit on
the top of AWT components and do their work

AWT components are platform Swing components are made in purely in java
dependent and they are platform independent

AWT components require java.awt Swing components require java. swing package
package
There are no different look and feel in There are different look and feel in Swing
AWT
AWT is a thin layer of code on top of Swing is much larger and has very much richer
the OS functionality

AWT provides less components than Swing provides more powerful


Swing. components such as tables, lists, scrollpanes,
colorchooser, tabbedpane etc.
Components of AWT and Swing
Every user interface considers the following three main aspects:

* UI Elements: These are the core visual elements the user


eventually sees and interacts with. GWT provides a huge list of
widely used and common elements varying from basic to
complex, which we will cover in this tutorial.

* Layouts: They define how UI elements should be organized on


the screen and provide a final look and feel to the GUI
(Graphical User Interface).

* Behavior: These are the events which occur when the user
interacts with UI elements. This part will be covered in the
Event Handling chapter
Every SWING controls inherits properties from the following Component
class hierarchy.

* Component: A Component is the abstract base class for the non-menu


user-interface controls of SWING.

* Component represents an object with graphical representation.


* Container: A Container is a component that can contain other SWING
components.

* JComponent: A JComponent is a base class for all SWING UI components.


* In order to use a SWING component that inherits from JComponent, the
component must be in a containment hierarchy whose root is a top-level
SWING container
Containers
* Containers are an integral part of SWING GUI components. A container provides
a space where a component can be located.

* A Container in AWT is a component itself and it provides the capability to add a


component to itself.

Following are certain noticeable points to be considered.

* Sub classes of Container are called as Container. For example, JPanel, JFrame
and JWindow.

* Container can add only a Component to itself.


* A default layout is present in each container which can be overridden using
setLayout method.

* Following is the list of commonly used containers while designed GUI using
SWING.
Container Container description
Name
JFrame A JFrame is a top-level window with a title and a
border. Each program that uses Swing components
has at least one top-level container, which is the root
of the containment hierarchy .
JPanel JPanel is the simplest container. It provides space in
which any other component can be placed, including
other panels.
JWindow A JWindow object is a top-level window with no
borders and no menu bar.
No Constructor & Description
JFrame ()
Constructs a new frame that is initially invisible.

JFrame (GraphicsConfigurationgc)
Creates a Frame in the specified GraphicsConfiguration of a screen device and
a blank title.
JFrame (String title)
Creates a new, initially invisible Frame with the specified title.

JFrame (String title, GraphicsConfigurationgc)


Creates a new JFrame with the specified title GraphicsConfiguration of
a screen device.

This class inherits methods from the following classes:


•java.awt.Frame
•java.awt.Window
•java.awt.Container
•java.awt.Component
•java.lang.Object
Commonly used JFrame class methods
No Constructor & Description
void setVisible (Boolean visible)
sets the visibility of the frame. By default, its false means it’s not visible
void setLayout (LayoutManager manager)
Sets the LayoutManager. By default, layout of JFrame is Flow Layout
void setDefaultCloseOperation (int operation)
Sets the operation that will happen by default when the user initiates a "close" on this frame.
void add (Component comp)
Adds the specified component to the frame
void remove (Component comp)
Removes the specified component from the container.
void removeAll ()
Removes every component from the container.
void setJMenuBar (JMenuBarmenubar)
Sets the menubar for this frame.
void setAlwaysOnTop (Boolean top)
sets whether the frame should be displayed always on top of other applications frame
void setTitle (String title)
Sets the title of the frame
String getTitle ()
Returns the title of the frame
Example .The following java program creates and displays JFrame to the user.
import javax.swing.JFrame;
public class MyFrame {
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("MyFrame");
frame.setSize(400, 300); // Set the frame size
// Center a frame
frame.setLocationRelativeTo(null); //or .setLocation(300,200)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); // Display the frame
}
Controls

* Components are the basic building block of an application.


Swing and AWT have a wide range of various components or
controls including buttons, check boxes, sliders and list
boxes.
Control Name Control Description
JLabel A JLabel object is a component for placing text in a container.
JButton This class creates a labeled button.
JColorChooser A JColorChooser provides a pane of controls designed to allow a user to manipulate
and select a color.
JCheckBox A JCheckBox is a graphical component that can be in either an on(true) or off
(false) state.
JRadioButton The JRadioButton class is a graphical component that can be in either an on (true)
or off (false) state. in a group.
JList A JList component presents the user with a scrolling list of text items.
JComboBox A JComboBox component presents the user with a to show up menu of choices.
JTextField A JTextField object is a text component that allows editing of a single line of text.
JPasswordField A JPasswordField object is a text component specialized for password entry.
JTextArea A JTextArea object is a text component that allows editing of a multiple lines of
text.
JScrollbar A Scrollbar control represents a scroll bar component in order to enable the user to
select from a range of values.
JOptionPane JOptionPane provides a set of standard dialog boxes that prompt the users for a
value or informs them of something.
JFileChooser A JFileChooser control represents a dialog window from which the user can select a
file.
JProgressBar As the task progresses towards completion, the progress bar displays the task's
percentage of completion.
Layout Managers

* When building a GUI, each GUI component must be attached to


a container, such as a window created with a JFrame.

* We typically must decide where to position each GUI


component. This is known as specifying the layout of the GUI
components. Java provides several layout managers that can
help us position components. Method setLayout is used and is
inherited from Container.

* The argument to the method must be an object of a class that


implements the LayoutManager interface
No LayoutManager & Description
BorderLayout: The borderlayout arranges the components to fit in the five
regions: east, west, north, south, and center.

FlowLayout:TheFlowLayout is the default layout. It lays out the components in a


directional flow.

GridLayout: The GridLayout manages the components in the form of a


rectangular grid.

GridBagLayout:This is the most flexible layout manager class. The object of


GridBagLayout aligns the component vertically, horizontally, or along their
baseline without requiring the components of the same size.

GroupLayout:TheGroupLayout hierarchically groups the components in order to


position them in a Container.
Event Handlers and Listeners
* When the user interacts with a GUI component, the
interaction—known as an event—drives the program to
perform a task.
* Some common events (user interactions) that might cause an
application to perform a task include clicking a button,
typing in a text field, selecting an item from a menu, closing
a window and moving the mouse.
* The code that performs a task in response to an event is
called an event handler and the overall process of
responding to events is known as event handling.

* Event handling can be accomplished using two ways by using


anonymous inner class and by creating event handler class
that implements the appropriate interface.
* Steps Required to Set Up Event Handling for a GUI Component
* Before an application can respond to an event for a particular GUI
component, we must perform several coding steps:
1. Create a class that represents the event handler.
2. Implement an appropriate interface, known as an event-listener
interface, in the class from Step 1.
3. Indicate that an object of the class from Steps 1 and 2 should be
notified when the event occurs. This is known as registering the event
handler.  
* Non-static nested classes are called inner classes and are frequently used
for event handling.
* When the user presses Enter in a JTextField or JPasswordField, the GUI
component generates an ActionEvent(package java.awt.event).
* Such an event is processed by an object that implements the interface
ActionListener (package java.awt.event).
* Every control should be registered in order to generate event for the
control.
*Following is the list of commonly used Event classes.
No Event Class Name

KeyEvent
On entering the character, the Key event is generated.

MouseEvent
This event indicates a mouse action occurred in a component.

WindowEvent
The object of this class represents the change in the state of a window.

AWTEventIt is the root event class for all SWING events. This class and its
subclasses replace the original java.awt.Eventclass.

ComponentEvent
The object of this class represents the change in the state of a window.

ContainerEvent
The object of this class represents the change in the state of a window.

MouseMotionEvent
The object of this class represents the change in the state of a window.
OU
K Y
A N
TH
RY
VE
! !
C H
MU

03/18/2021

You might also like