Using AWT Controls, Layouts Managers, and Menus
Using AWT Controls, Layouts Managers, and Menus
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
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.
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:
We can set or change the text in a label by using the setText() method and obtain
the current label by calling getText().
We can set the alignment of the string within the label by calling setAlignment()
and to obtain the alignment we use getAlignment():
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
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
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
Swing uses the same events as does the AWT, and these events are packaged
in java.awt.event.
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.
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