Swings Listeners and Layout
Swings Listeners and Layout
Component
Button
Checkbox
TextComponent
TextArea
TextField
Choice
List
Container
Panel
Window
ScrollPane
Applet
Dialog
Label
Canvas
ScrollBar
Frame
Layout Managers
Dialog
Panel
Applet
ScrollPane
Containers
Method defined in class Container include:
setLayoutManager (LayoutManager)
add (Component)
remove(Component)
Containers
Window
A Window is a 2-dimensional drawing surface that can be displayed
on an output device.
A Window can be stacked on top of other Windows and can be
moved to the front or back of the visible windows.
Methods in class Window include:
show ( )
toFront( )
toBack( )
Frame
A Frame is a type of Window with a title bar, a menu bar, a border, and a
cursor. It is used most frequently to display applications that contain a
graphical user interface or an animation.
getTitle(String)
setCursor(int)
setResizable( )
setMenuBar(MenuBar)
Containers
Panel
Dialog
BorderLayoutManager (same as
ScrollPane
Layout Managers
Flow Layout
Lays out components row by row from left to right.
It is very straightforward to use, but it affords the programmer the
least ability to control the positioning of components. It is the
default layout manager in an applet.
Layout Managers
English
French
Japanese
Spanish
Arabic
Greek
German
Portuguese
Chinese
Applet Started
Russian
Layout Managers
Constructing the previous display
import java.awt.*;
public class SelectLang extends java.applet.Applet {
Button eng = new Button(English);
.
Button rus = new Button (Russian);
FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
public void init ( ) {
setLayout(flow);
add(eng);
.
add(rus);
}
}
Layout Managers
Suppose we wish to construct the following layout
Age
Under 20
21-35
36-65
Name
Street Address
City
Zip
Over 65
Layout Managers
Layout Managers
Border Layout
North
West
Center
South
East
Layout Managers
Properties of a Border Layout Manager
Layout Managers
Example A BorderLayout with a Label in North, a Scrollbar in South,
Red
Blue
Green
Black
White
Rectangle
Oval
Line
Fill
Background
Layout Managers
Grid Layout
Layout Managers
Grid Layout
Layout Managers
Grid Layout
name
address
Layout Managers
Card Layout
A card layout is a group of components or
containers that are displayed one at a time.
Layout Managers
Card Layout Construction
Card Panel
Layout Managers
Applet Declarations
public class CardDemo extends Applet {
private final int numCards = __;
//whatever the number
private String[ ] = new CardLabels[numCards];
//construct an array for holding CardPanel objects specified in separate file
private CardPanel[ ] cards = new CardPanel[numCards];
private CardLayout theLayout;
//Layout for CardPanels
//Create a Panel for displaying CardPanel objects in the Applet (green area)
private Panel cardDisplayPanel;
Applet initialization
public void init ( ) {
//essential statements for card display
setLayout (new BorderLayout);
//layout for the Applet
add(West, makeOtherPanel( )); //2nd parameter returns a Panel
add(Center, makeCardDisplayPanel( ));
//2nd param returns a Panel
}
Layout Managers
CardPanel Objects
CardPanel is a separately declared class that extends Panel
CardPanels are containers with their own Components and
Layout
CardPanel objects are created in method makeCardDisplayPanel() of
Applet
Swing architecture is rooted in the model-viewcontroller ( MVC) design that dates back to SmallTalk .
MVC architecture calls for a visual application to be broken
up into three separate parts:
A model that represents the data for the application.
Text-based interface
Predetermined
sequence of events
The program pauses
execution when it
expects input from the
user and continues on
the same set path after
it receives the input
Graphical interface
No set sequence of
events
The user can do any
number of things at
any time (type in a text
box, resize the
window, press a
button)
These responses are
called events.
We say the program is
event-driven.
AWT
AWT stands for Abstract windows
toolkit.
SWING
Swing is also called as JFCs (Java Foundation classes).
Swings are called light weight component because swing
components sits on the top of AWT components and do the work.
Swing components require javax.swing package.
Swing components are made in purely java and they are platform
independent.
We can have different look and feel in Swing.These feature is not
available in AWT.Swing has many advanced features like JTabel,
Jtabbed pane which is not available in AWT. Also. Swing
components are called "lightweight" because they do not require a
native OS object to implement their functionality. JDialog and
JFrame are heavyweight, because they do have a peer. So
components like JButton, JTextArea, etc., are lightweight because
they do not have an OS peer.
With Swing, you would have only one peer, the operating system's
window object. All of the buttons, entry fields, etc. are drawn by the
Swing package on the drawing surface provided by the window
object. This is the reason that Swing has more code. It has to draw
the button or other control and implement its behavior instead of
relying on the host operating system to perform those functions.
Swing is much larger. Swing also has very much richer functionality.
Constructors
These three constructors apply to checkboxes that operate
29
30
getState/setState
Retrieves or sets the state of the checkbox: checked (true)
or unchecked (false)
getLabel/setLabel
Retrieves or sets the label of the checkbox
After changing the label invalidate and validate the
addItemListener/removeItemListener
Add or remove an ItemListener to process
ItemEvents in itemStateChanged
INE2720 Web Application
Software Development
31
CheckboxGroup Constructors
CheckboxGroup()
Creates a non-graphical object used as a tag to group checkboxes
logically together
Only one checkbox associated with a particular tag can be selected
at any given time
Checkbox Constructors
Checkbox(String label, CheckboxGroup group,
boolean state)
Creates a radio button associated with the specified group, with the
given label and initial state
INE2720 Web Application
Software Development
32
import java.applet.Applet;
import java.awt.*;
public class CheckboxGroups extends Applet {
public void init() {
setLayout(new GridLayout(4, 2));
setBackground(Color.lightGray);
setFont(new Font("Serif", Font.BOLD, 16));
add(new Label("Flavor", Label.CENTER));
add(new Label("Toppings", Label.CENTER));
CheckboxGroup flavorGroup = new CheckboxGroup();
add(new Checkbox("Vanilla", flavorGroup, true));
add(new Checkbox("Colored Sprinkles"));
add(new Checkbox("Chocolate", flavorGroup, false));
add(new Checkbox("Cashews"));
add(new Checkbox("Strawberry", flavorGroup, false));
add(new Checkbox("Kiwi"));
}
}
INE2720 Web Application
Software Development
33
Constructors
List(int rows, boolean multiSelectable)
Creates a listbox with the specified number of visible rows
Depending on the number of item in the list (addItem or add), a
scrollbar is automatically created
The second argument determines if the List is multiselectable
List()
Creates a single-selectable list box with a platform-dependent
number of rows and a platform-dependent width
List(int rows)
Creates a single-selectable list box with the specified number of
rows and a platform-dependent width
INE2720 Web Application
Software Development
34
import java.awt.*;
public class Lists extends CloseableFrame {
public Lists() {
super("Lists");
setLayout(new FlowLayout());
setBackground(Color.lightGray);
setFont(new Font("SansSerif", Font.BOLD, 18));
List list1 = new List(3, false);
list1.add("Vanilla");
list1.add("Chocolate");
list1.add("Strawberry");
add(list1);
List list2 = new List(3, true);
list2.add("Colored Sprinkles");
list2.add("Cashews");
list2.add("Kiwi");
add(list2);
pack();
setVisible(true);
}}
35
EVENT HANDING
EventObject(java.util)
AWTEvent(java.awt)
void menuCanceled(MenuEvent e)
void menuDeselected(MenuEvent e)
void menuSelected(MenuEvent e)