0% found this document useful (0 votes)
54 views

Using AWT Controls, Layouts Managers, and Menus

This document discusses Swing, the Java GUI toolkit. It begins by explaining that Swing is built on top of AWT and is written entirely in Java. It then compares Swing to AWT, noting that Swing is platform independent, lightweight, supports pluggable look and feel, and provides more powerful components. The document outlines key Swing features like the MVC model and discusses components, containers, packages, and event handling in Swing.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Using AWT Controls, Layouts Managers, and Menus

This document discusses Swing, the Java GUI toolkit. It begins by explaining that Swing is built on top of AWT and is written entirely in Java. It then compares Swing to AWT, noting that Swing is platform independent, lightweight, supports pluggable look and feel, and provides more powerful components. The document outlines key Swing features like the MVC model and discusses components, containers, packages, and event handling in Swing.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Chapter 26

Using AWT controls,


layouts managers, and
menus
2

INTRODUCTION
In this chapter we look at several of the AWT’s
controls, how to create and handle buttons.
Controls are components that allow a user to
interact with the applications in various ways, a
commonly used control is a push button.
3

CONTROLS
AWT supports the following types of controls:
Labels, push buttons, check boxes, lists, scroll bars, text
editing etc.
All AWT controls are subclasses of Component. AWT
provides a small set of controls, perfect for simple
applications, for commercial applications, we use Swing
about which we’ll learn later.
4

ADDING AND REMOVING


CONTROLS
To include a control to a window, we must add it to the window using
the add add() method. First we create an instance of the desired control
and then add it to the window by calling add() using the form,
component add(component ref)
When a control is no longer needed we can remove it using remove()
method,
void remove (component ref)
5

RESPONDING TO CONTROLS
Bar labels, all controls generate events when accessed by the user. A
program simply implements the appropriate interface & then registers
an event listener for each control.

THE HEADLESS EXCEPTION


When an attempt is made to instantiate a GUI component in a non-
interactive environment(no display, mouse, keyboard is present) a
headless exception is thrown. This exception is useful when we need to
write code that can adapt to non-interactive environment.
6

LABELS
A label is an object of type Label, which contains a string which it displays. Labels
are passive controls which do not support any interaction with the user. Label
defines the following constructors:

Label() throws HeadlessException //creates blank label

Label(String str) throws HeadlessException /*creates label


containing string specified by str*/

Label(String str, int how) throws HeadlessException /* creates label that


contains string str using alignment specified by how*/

The value of how must be Label.LEFT, Label.RIGHT or Label.CENTER.


7

We can set or change the text in a label by using the setText() method and obtain
the current label by calling getText().

void setText(String str) //str specifies the new label

String getText() // the current label is returned

We can set the alignment of the string within the label by calling setAlignment()
and to obtain the alignment we use getAlignment():

void setAlignment(int how)

int getAlignment()
8

EXAMPLE
9

OUTPUT
10

USING BUTTONS
A push button is a component that contains a label and that generates an event when it is
pressed. Push buttons are objects of type Button. Button defines these two constructors:
Button( ) throws HeadlessException //creates an empty button.
Button(String str) throws HeadlessException //creates a button that contains str as a
label.
After a button has been created, you can set its label by calling
setLabel( ).
You can retrieve its label by calling getLabel( ). These methods are as follows:
void setLabel(String str)
String getLabel( ) Here, str becomes the new label for the button.
11

When a button is pressed, an action is generated.


It is sent to any listener interested in receiving action event notification from the
component.
Each listener implements ActionListener interface.
That interface defines the actionPerformed() method which is called when an event
occurs.
An ActionEvent object is supplied as the argument to this method.
It contains both a reference to the button that generated that event and a reference to
the action command String associated with the button.
By default action command string in the label of the button.
12

EXAMPLE
Here is an example that creates three buttons labeled "Yes", "No", and
"Undecided".
Each time one is pressed, a message is displayed that reports which button has
been pressed. In this version, the action command of the button (which, by
default, is its label) is used to determine which button has been pressed. The
label is obtained by calling the getActionCommand( ) method on the ActionEvent
object passed to actionPerformed( ).
13
14
15

OUTPUT
16

Chapter-31
Introducing
Swing
Topics
➔ What exactly is swing
➔ Difference between AWT & Swing
and why is swing better
➔ Key features of Swing
➔ The MVC connection
➔ Components & Containers
➔ Swing packages
➔ Event handling
➔ Painting in swing
What is Swing
Swing is a part of Java Foundation Classes (JFC) that is used to
create window-based applications. It is built on the top of AWT
(Abstract Windowing Toolkit) API and entirely written in java.

import
javax.swing.*;
Difference between
Swing
AWT and AWT Swing
➔ AWT components are ➔ Swing components are platform-
platform-dependent. independent.
➔ Heavyweight ➔ Lightweight
➔ Doesn’t support pluggable ➔ Supports pluggable look and feel
look and feel ➔ It provides more powerful
➔ Provides less components components such as tables, lists,
than swing scrollpanes, colorchooser etc.
➔ Doesn’t follow MVC ➔ Follows MVC
Then why is Swing
Swing was apreferred
response to deficiencies present in Java’s
original GUI subsystem: the Abstract Window
Toolkit(AWT).
AWT translates its various visual components into their
corresponding, platform specific equivalents, resulting in
variations in look and feel of the component.
Also the use of heavyweight components in AWT causes
some frustrating restrictions.
lightweight
➔ native (written in
java)
➔ components are
more efficient and
flexible

Two key features of Swing


Pluggable look and
feel (PLAF)
➔ end look and feel
depends on swing
and not on OS
➔ Rendering can
happen without
affecting other
components
The MVC Connection
Model-View-Controller

Model contains the information associated with the component


View determines how the component will be displayed
Controller determines how the component reacts to the user

Swing uses a modified version of MVC that combines the view and the
controller into a single logical entity and this approach is known the
Model-Delegate architecture.
Compon
A component is an object ents
having a graphical representation that can
be displayed on the screen and that can interact with the user: Like
buttons, checkboxes, slider and many more.
E.g. - JButton, JLabel, JApplet, JFrame

Most swing components are derived from the JComponent class


except for the 4 top level containers.
Swing
Components
All component classes start with the
letter J
Contain
ers
Containers are the interface between a component and the low-level,
platform-specific functionality that supports the component.
Basically, a container is a special type of component that is designed
to hold a group of components.

In order for a component to be displayed, it must be held within a


container.

There are 2 types of containers:

All containers 1) Top level containers or heavyweight


are also 2) Lightweight containers
components
Containe
rs
Top-level containers: JFrame, JApplet, JWindow, and JDialog

➔ Do not inherit JComponent class


➔ Are heavyweight

Lightweight containers: Lightweight containers are often used to organize


and manage groups of related components because a lightweight container
can be contained within another container + they do inherit JComponent
class.
Swing
Packages
Beginning the JDK 9, the Swing packages are part of the
java.desktop module. The main package is javax.swing. This
package must be imported into any program that uses Swing.
It contains the classes that implement the basic Swing
components, such as push buttons, labels, and check boxes.
Simple Swing
Code
Event
Handling
Swing components which react to user input generate events and that is when
event handling comes into play.

Swing uses the same events as does the AWT, and these events are packaged
in java.awt.event.

Events specific to Swing are stored in javax.swing.event.


Painting in
Swing
Painting in swing is built on the original AWT-based mechanism, but Swing’s implementation
offers more grained control.

Swing uses a bit more sophisticated approach to painting that involves three distinct
methods: paintComponent( ), paintBorder( ), and paintChildren( ).

To paint to the surface of a Swing component, you will create a subclass of the component
and then override its paintComponent( ) method.
protected void paintComponent(Graphics g)
When overriding paintComponent( ), the first thing you must do is call
super.paintComponent( )
Outputs
Chapter 32
EXPLORING
SWINGS
36

CONTENT
Here we’ll be doing discussion on Swing by
presenting an overview of several Swing
components,
such as Buttons,Labels,TextField,etc.
37

JLabel
JLabel is Swing’s easiest-to-use component.JLabel can be used to display text and/or an icon.
JLabel defines several
constructors. Here are three of them:

 JLabel(Icon icon)
 JLabel(String str)
 JLabel(String str, Icon icon, int align)
38

ImageIcon
Icons are specified by objects of type Icon, which is an interface
defined by Swing. The easiest way to obtain an icon is to use the
ImageIcon
class. ImageIcon implements Icon and encapsulates an image.
Thus, an object
of type ImageIcon can be passed as an argument to the Icon
parameter of
JLabel’s constructor.
ImageIcon(String filename)
39

CODE
40

OUTPUT
41

JTextField
JTextField is used to get one line of text from the user.
It generates events in response to user interaction, allowing us to retrieve the string
from the user at runtime, and can be treated in a desired way.

Three of JTextField’s Constructor are shown here:


• JTextField(int cols)
• JTextField(String str, int cols)
• JTextField(String str)
42

CODE
43

OUTPUT
44

JButton
The JButton class provides the functionality of a push button. JButton allows an
icon, a string, or both to be associated with the push button.It is unidirectional, i.e.
once it is pushed, it cannot be unpushed. Three of its
constructors are shown here:

 JButton(Icon icon)
 JButton(String str)
 JButton(String str, Icon icon)
45

CODE
46

OUTPUT
47

JToggleButton
It is nothing but a variation of push button.A toggle button has two states:
pushed and released. That is, when you press a toggle button, it stays pressed
rather than popping back up as a regular push button does. When you press the
toggle button a second time, it releases (pops up). Therefore, each time a toggle
button is pushed, it toggles between its two states.
Constructor we are using for this is:

 JToggleButton(String str)

We can use ItemListener to get the state of button using boolean isSelected().
48

CODE
49

CODE
50

OUTPUT
51

OUTPUT
52

THANK YOU

You might also like