Checkbox
• A check box is a control that is used to turn an option on or off
• It consists of a small box that can either contain a check mark or not.
• There is a label associated with each check box that describes what option the box
represents the state of a check box can be changed by clicking on it
Constructors
Checkbox( ) Creates a blank checkbox
Checkbox(String str) creates a checkbox with string label
Checkbox(String str, boolean on) creates a checkbox with string label and value of on as true/false
Checkbox(String str, boolean on, CheckboxGroup cbGroup)
Checkbox(String str, CheckboxGroup cbGroup, boolean on)
Methods
boolean getState( )
void setState(boolean on)
String getLabel( )
void setLabel(String str)
• Each time a check box is selected or deselected, an item event is generated
• The event is sent to any listeners that previously registered an interest in receiving item
event notifications from that component.
• Each listener implements the ItemListener interface
• That interface defines the itemStateChanged( ) method
• An ItemEvent object is supplied as the argument to this method
• It is possible to create a set of mutually exclusive check boxes in which one and only one
check box in the group can be checked at any one time.
• These check boxes are often called radio buttons
• To create a set of mutually exclusive check boxes, first define the group to which they will
belong and then specify that group when the check boxes are constructed.
• Check box groups are objects of type CheckboxGroup
• Constructors
CheckboxGroup()
• Methods
Checkbox getSelectedCheckbox( )
void setSelectedCheckbox(Checkbox which)- which check box in a group is currently selected
Choice
• Choice is used to create a pop-up list of items from which the user may choose
• When inactive, a Choice component takes up only enough space to show the currently
selected item
• When the user clicks on it, the whole list of choices pops up, and a new selection can be
made
Each item in the list is a string that appears as a left-justified label in the order it is added to the
Choice object
• Constructors
Choice() – creates a choice with no elements
• Methods
void add(String name) – used to add an element to choice.
String getSelectedItem( ) – returns the item selected
int getSelectedIndex( ) – returns the index of the item selected. First index is zero
int getItemCount( ) - return the total number of items in the choice
void select(int index) – used to select an item using index
void select(String name) – used to select an item using name
String getItem(int index) – returns the item associated with the index
List
• This class provides a compact, multiple-choice, scrolling selection list.
• A List object can be constructed to show any number of choices in the visible window.
• It can also be created to allow multiple selections
• Constructors
List( )-creates a List control that allows only one item to be selected at any one time
List(int numRows) - the value of numRows specifies the number of entries in the list that will
always be visible
(others can be scrolled into view as needed).
List(int numRows, boolean multipleSelect)
if multipleSelect is true, then the user may select two or
more items at a time. If it is false, then only one item may
be selected.
• Methods
void add(String name)
name is item added to the list.
Items are added to the end of the list
void add(String name, int index)
The second form adds the item at the index specified by index.
Indexing begins at zero. –1 adds the item to the end of the list.
String getSelectedItem( ) returns a string containing the name of the item. If more than one item
is selected, or if no selection has yet been made, null is returned
int getSelectedIndex( ) returns the index of the item. The
first item is at index 0. If more than one item is selected,
or if no selection has yet been made, –1 is returned
String[ ] getSelectedItems( ) – returns the items selected
int[ ] getSelectedIndexes( )- returns the indexes of the items selected
int getItemCount( ) –returns the total number of items in the list
void select(int index) – selects the item in the index position
String getItem(int index) – returns the item associated with the index position
• To process list events, implement the ActionListener interface.
• Each time a List item is double-clicked, an ActionEvent object is generated.
• Its getActionCommand( ) method can be used to retrieve
the name of the newly selected item
• each time an item is selected or deselected with a single
click, an ItemEvent object is generated.
Its getStateChange( ) method can be used to determine
whether a selection or deselection triggered this event
Scrollbar
• Scroll bars are used to select continuous values between a specified minimum and maximum
• Scroll bars may be oriented horizontally or vertically
• Each end has an arrow that you can click to move the current
value of the scroll bar one unit in the direction of the arrow
• The current value of the scroll bar relative to its minimum
and maximum values is indicated by the slider box (or
thumb) for the scroll bar
• The slider box can be dragged by the user to a new position
• Constructors
Scrollbar( ) – creates a vertical scrollbar
Scrollbar(int style) –style -Scrollbar.VERTICAL, Scrollbar.HORIZONTAL
Scrollbar(int style, int initialValue, int thumbSize, int min, int max)
initial value of the scroll bar is passed in initialValue.
number of units represented by the height of the thumb is
passed in thumbSize.
minimum and maximum values for the scroll bar are
specified by min and max.
• Methods
void setValues(int initialValue, int thumbSize, int min, int max)
Sets the values if the first two constructors are used
int getValue( ) –returns the current value of scrollbar
void setValue(int newValue) – sets a new value
int getMinimum( ) – returns the minimum value
int getMaximum( ) – returns the maximum value
• To process scroll bar events, implement the AdjustmentListener interface.
• It defines the method adjustmentValueChanged()
TextField
• The TextField class implements a single-line text-entry area, usually called an edit control.
• Text fields allow the user to enter strings and to edit the text using the arrow keys, cut and
paste keys, and mouse selections.
TextField is a subclass of TextComponent
• Constructors
TextField( ) – creates a default textfield
TextField(int numChars) – creates a textfield numChars wide
TextField(String str) – creates a textfield with the string str contained in it
TextField(String str, int numChars)
Create a textfield with str in it and numChars specifies width
• Methods
String getText( ) - returns the string in text field
void setText(String str) – sets the string in the text field
String getSelectedText( ) - returns the selected text
void select(int startIndex, int endIndex) – selects the text from startIndex to endIndex-1
boolean isEditable( ) – returns true if the text field is editable
void setEditable(boolean canEdit) –sets if the text field is editable
void setEchoChar(char ch) – set echo char to reflect for key stroke
boolean echoCharIsSet( ) – returns true is echo char is set
char getEchoChar( ) – returns the echo char
TextArea
• TextArea is a multiline editor
• Constructors:
TextArea( ) creates a multi line editor
TextArea(int numLines, int numChars) – numLines – rows, numChars - columns
TextArea(String str)- creates text area with string value
TextArea(String str, int numLines, int numChars)
TextArea(String str, int numLines, int numChars, int sBars)
sBars - scrollbars
• void append(String str)
• void insert(String str, int index)
void replaceRange(String str, int startIndex, int endIndex)
MouseListener
Methods
void mouseClicked(MouseEvent me)
void mouseEntered(MouseEvent me)
void mouseExited(MouseEvent me)
void mousePressed(MouseEvent me)
void mouseReleased(MouseEvent me)
MouseMotionListener
Methods
void mouseDragged(MouseEvent me)
void mouseMoved(MouseEvent me)
KeyListener
void keyPressed(KeyEvent ke)
void keyReleased(KeyEvent ke)
void keyTyped(KeyEvent ke)
WindowListener
void windowActivated(WindowEvent we)
void windowDeactivated(WindowEvent we)
void windowIconified(WindowEvent we)
void windowDeiconified(WindowEvent we)
void windowOpened(WindowEvent we)
void windowClosing(WindowEvent we)
void windowClosed(WindowEvent we)
LayoutManager
A layout manager automatically arranges the controls within a window by using some type of
algorithm
Each Container object has a layout manager associated with it
A layout manager is an instance of any class that implements the LayoutManager interface
The layout manager is set by the setLayout( ) method.
If setLayout() is not invoked, then the default layoutmanager
is used
Whenever a container is resized (or sized for the first time),
the layout manager is used to position each of the components
within it
FlowLayout
FlowLayout is the default layout manager of the Applet
It implements a simple layout style, which is similar to how words flow in a text editor
By default, components are laid out line-by-line beginning at the upper-left corner
When a line is filled, layout advances to the next line.
• Constructors
FlowLayout( ) - creates the default layout, which centers components and leaves five pixels of
space between each component
FlowLayout(int how) –specifies how each line is aligned. Valid values for how are as follows:
FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT
FlowLayout.LEADING
FlowLayout.TRAILING
FlowLayout(int how, int horz, int vert)
horz, vert specifies the space in pixels horizontally and vertically
aroud each object
BorderLayout
The BorderLayout class implements a common layout style for top-level windows.
It has four narrow, fixed-width components at the edges and one large area in the center.
The four sides are referred to as north, south, east, and west. The middle area is called the
center.
Constructors
BorderLayout( )
BorderLayout(int horz, int vert)
Constants defined in BorderLayout class
NORTH, SOUTH, EAST, WEST, CENTER
GridLayout
• GridLayout lays out components in a two-dimensional grid
• Constructors
GridLayout( ) – adds components one after the other in a row
GridLayout(int numRows, int numColumns) – creates a grid of rowXcolumn
GridLayout(int numRows, int numColumns, int horz, int vert)
Creates rowXcolumn with horizontal and vertical space
specified
CardLayout
• The CardLayout class is unique among the other layout managers in that it stores several
different layouts
• Each layout can be thought of as being on a separate index card in a deck that can be
shuffled so that any card is on top at a given time
• The cards are typically held in an object of type Panel
• This panel must have CardLayout selected as its layout
• The cards that form the deck are also typically objects of
type Panel
• A panel must be created that contains the deck and also a panel
for each card in the deck
• Constructors
CardLayout( )
CardLayout(int horz, int vert)
Methods
void add(Component panelObj, Object name)
void first(Container deck)
void last(Container deck)
void next(Container deck)
void previous(Container deck)
void show(Container deck, String cardName)
GridBagLayout
• This is useful as it allows to specify the relative placement of components by specifying their
positions within cells inside a grid.
• The key to the grid bag is that each component can be a different size, and each row in the
grid can have a different number of columns
• The location and size of each component in a grid bag are
determined by a set of constraints linked to it.
• The constraints are contained in an object of type
“GridBagConstraints”
Constructors
GridBagLayout( )
Methods
void setConstraints(Component comp, GridBagConstraints cons)
comp is the component for which the constraints cons apply
Frame
• Frame encapsulates what is commonly thought of as a “window.”
• It is a subclass of Window and has a title bar, menu bar, borders, and resizing corners
Menu
• A top-level window can have a menu bar associated with it.
• A menu bar displays a list of top-level menu choices.
• Each choice is associated with a drop-down menu.
• This concept is implemented in the AWT by the following classes: MenuBar, Menu, and
MenuItem
• A menu bar contains one or more Menu objects
• Each Menu object contains a list of MenuItem objects.
• Each MenuItem object represents something that can be
selected by the user.
• Constructors
MenuBar( )
Method
Menu add(Menu menu)
Menu( )
Menu(String optionName) – name of the menu
Menu(String optionName, boolean removable) if true, then
menu can be removed and allowed to float, if false, it will be
Attached to the menu bar.
Method
MenuItem add(MenuItem item)
MenuItem( )
MenuItem(String itemName)
MenuItem(String itemName, MenuShortcut keyAccel)
itemName is the name shown in the menu, and keyAccel is the menu shortcut for this item
Methods of MenuItem
void setEnabled(boolean enabledFlag)
boolean isEnabled( )
void setLabel(String newName)
String getLabel( )
CheckboxMenuItem( )
CheckboxMenuItem(String itemName)
CheckboxMenuItem(String itemName, boolean on)
Methods
boolean getState( )
void setState(boolean checked)
Dialog
• Dialog boxes are primarily used to obtain user input and are often child windows of a top-
level window.
• Dialog boxes may be modal or modeless.
• When a modal dialog box is active, all input is directed
to it until it is closed.
• This means that you cannot access other parts the program until you have closed the dialog
box.
• When a modeless dialog box is active, input focus can be
directed to another window in the program
Constructor
Dialog(Frame parentWindow, boolean mode)
Dialog(Frame parentWindow, String title, boolean mode)
parentWindow is the owner of the dialog box.
If mode is true, the dialog box is modal else it is modeless.
title of the dialog box can be passed in title
FileDialog
Java provides a built-in dialog box that lets the user specify a file.
To create a file dialog box, instantiate an object of type FileDialog
Constructors
FileDialog(Frame parent)
FileDialog(Frame parent, String boxName)
FileDialog(Frame parent, String boxName, int how)
parent is the owner of the dialog box.
boxName specifies the name displayed in the box’s title bar.
If boxName is omitted, the title of the dialog box is empty.
If how is FileDialog.LOAD, then the box is selecting a file for
reading.
If how is FileDialog.SAVE, the box is selecting a file for writing
If how is omitted, the box is selecting a file for reading