Chapter 10 Components
· Java’s building blocks for
creating GUIs.
· All non-menu related
components inherit from java.awt.Component, that provides basic support for
event handling, controlling component size, color, font and drawing of
components and their contents.
· Component class implements
ImageObserver, MenuContainer and Serializable interfaces. So all AWT components
can be serialized and can host pop-up menus.
· Component methods:
|
Controls |
Methods /
Description |
|
Size |
Dimension
getSize() void
setSize(int width, int height) void
setSize(Dimension d) |
|
Location |
Point
getLocation() void
setLocation(int x, int y) void
setLocation(Point p) |
|
Size and
Location |
Rectangle
getBounds() void
setBounds (int x, int y, int width, int height) void
setBounds (Rectangle r) |
|
Color |
void
setForeground(Color c) void
setBackground(Color c) |
|
Font |
void
setFont(Font f) void
setFont(Font f) |
|
Visibility
and Enabling |
void
setEnabled(boolean b) void
setVisible(boolean b) |
· Container class extends
Component. This class defines methods for nesting components in a container.
Component
add(Component comp)
Component
add(Component comp, int index)
void
add(Component comp, Object constraints)
void
add(Component comp, Object constraints, int index)
void
remove(int index)
void
remove(Component comp)
void
removeAll()
· The following are the
containers:
|
Container |
Description |
|
Panel |
·
Provides
intermediate level of spatial organization and containment. ·
Not a
top-level window ·
Does not
have title, border or menubar. ·
Can be
recursively nested. ·
Default
layout is Flow layout. |
|
Applet |
·
Specialized
Panel, run inside other applications (typically browsers) ·
Changing
the size of an applet is allowed or forbidden depending on the browser. ·
Default
layout is Flow layout. |
|
Window |
·
Top-level
window without a title, border or menus. ·
Seldom used
directly. Subclasses (Frame and Dialog) are used. ·
Defines
these methods: ·
void pack()
– Initiates layout management, window size might be changed as a result ·
void show()
– Makes the window visible, also brings it to front ·
void
dispose() – When a window is no longer needed, call this to free
resources. |
|
Frame |
·
Top-level
window (optionally user-resizable and movable) with a title-bar, an icon
and menus. ·
Typically
the starting point of a GUI application. ·
Default
layout is Border layout. |
|
Dialog |
·
Top-level
window (optionally user-resizable and movable) with a title-bar. ·
Doesn’t
have icons or menus. ·
Can be made
modal. ·
A parent
frame needs to be specified to create a Dialog. ·
Default
layout is Border layout. |
|
ScrollPane |
·
Can contain
a single component. If the component is larger than the scrollpane, it
acquires vertical / horizontal scrollbars as specified in the constructor. ·
SCROLLBARS_AS_NEEDED – default, if nothing specified ·
SCROLLBARS_ALWAYS ·
SCROLLBARS_NEVER |
· Top-level containers (Window,
Frame and Dialog) cannot be nested. They can contain other containers and other
components.
· GUI components:
Component
|
Description
|
Constructors
|
Events
|
|
Button |
· A
button with a textual label. |
new Button(“Apply”) |
Action event. |
|
Canvas |
· No
default appearance. · Can
be sub-classed to create custom drawing areas. |
|
Mouse, MouseMotion, Key events. |
|
Checkbox |
·
Toggling check box. ·
Default initial state is false. ·
getState(), setState(boolean state) - methods · Can
be grouped with a CheckboxGroup to provide radio behavior. ·
Checkboxgroup is not a subclass of Component. ·
Checkboxgroup provides these methods: getSelectedCheckbox and
setSelectedCheckbox(Checkbox new) |
Checkbox(String label) Checkbox(String label, boolean initialstate) Checkbox(String label, CheckBoxGroup group) |
Item event |
|
Choice |
· A
pull-down list · Can
be populated by repeatedly calling addItem(String item) method. · Only
the current choice is visible. |
|
Item event |
|
FileDialog |
·
Subclass of Dialog · Open
or Save file dialog, modal ·
Dialog automatically removed, after user selects the file or hits cancel. ·
getFile(), getDirectory() methods can be used to get information about the
selected file. |
FileDialog(Frame parent, String title, int
mode) Mode can be FileDialog.LOAD or FileDialog.SAVE |
|
|
Label |
·
Displays a single line of read-only non-selectable text ·
Alignment can be Label.LEFT, Label.RIGHT
or Label.CENTER |
Label() Label(String label) Label(String label, int align) |
None |
|
List |
·
Scrollable vertical list of text items. · No of
visible rows can be specified, if not specified layout manager determines
this. ·
Acquires a vertical scrollbar if needed. · List
class methods: ·
addItem(String), addItem(String, int index) ·
getItem(int index), getItemCount() ·
getRows() – no of visible rows · int
getSelectedIndex() · int[]
getSelectedIndexes() ·
String getSelectedItem() ·
String[] getSelectedItems() |
List() List(int nVisibleRows) List(int nVisibleRows, boolean
multiSelectOK) |
Item event – selecting or deselecting Action event – double clicking |
|
Scrollbar |
· With
the last form of constructor, calculate the spread as maxvalue – minvalue.
Then the slider width is slidersize / spread times of scrollbar width. |
Scrollbar() – a vertical scrollbar. Scrollbar(int orientation) Scrollbar(int orientation, int initialvalue,
int slidersize, int minvalue, int maxvalue) Orientation can be Scrollbar.HORIZONTAL Scrollbar.VERTICAL |
Adjustment event |
|
TextField |
·
Extends TextComponent ·
Single line of edit / display of text. ·
Scrolled using arrow keys. ·
Depending on the font, number of displayable characters can vary. · But,
never changes size once created. ·
Methods from TextComponent: ·
String getSelectedText() ·
String getText() · void
setEditable(boolean editable) · void
setText(String text) |
TextField() – empty field TextField(int ncols) – size TextField(String text) – initial text TextField(String text, int ncols) – initial
text and size |
Text event |
|
TextArea |
·
Extends TextComponent ·
Multiple lines of edit/display of text. ·
Scrolled using arrow keys. · Can
use the TextComponent methods specified above. ·
Scroll parameter in last constructor form could be
TextArea.SCROLLBARS_BOTH, TextArea.SCROLLBARS_NONE,
TextArea.SCROLLBARS_HORIZONTAL_ONLY TextArea.SCROLLBARS_VERTICAL_ONLY |
TextArea() – empty area TextArea(int nrows, int ncols) – size TextArea(String text) – initial text TextArea(String text, int nrows, int ncols)
– initial text and size TextArea(String text, int nrows, int ncols,
int scroll) |
Text event |
· Pull-down menus are accessed via a menu
bar, which can appear only on Frames.
· All menu related components inherit from
java.awt.MenuComponent
· Steps to create and use a menu
· Create an instance of
MenuBar class
· Attach it to the frame – using
setMenubar() method of Frame
· Create an instance of Menu and populate
it by adding MenuItems, CheckboxMenuItems, separators and Menus. Use
addSeparator() method to add separators. Use add() method to add other items.
· Attach the Menu to the MenuBar. Use add()
method of Menubar to add a menu to it. Use setHelpMenu to set a particular menu
to appear always as right-most menu.
· Menu(String label) – creates a Menu
instance. Label is what displayed on the Menubar. If this menu is used as a
pull-down sub-menu, label is the menu item’s label.
· MenuItems generate Action Events.
· CheckboxMenuItems generate Item Events.
