System.gc(): It asks the garbage collector to run at any time by calling System's gc() method
UNIT 7: INTRODUCTION TO APPLET PROGRAMMING
8.6 How to implement event handlers:
1. Code declaring that the class implements a listener interface : public class MyClass implements ActionListener {
2. Code that registers an instance of the event handling class as a listener:
someComponent.addActionListener(instanceOfMyClass);
1. The implementation of the methods in the listener interface:
public void actionPerformed(ActionEvent e) {
...//code that reacts to the action...
}
8.6.1 Buttons
The Button class provides a default button implementation. A button is a simple control that generates an action event when the user clicks it.
Code Example:
b1 = new Button();
b1.setLabel("Disable middle button");
b1.setActionCommand(DISABLE);
b2 = new Button("Middle button");
b3 = new Button("Enable middle button");
b3.setEnabled(false);
b3.setActionCommand(ENABLE);
//Listen for actions on buttons 1 and 3.
b1.addActionListener(this);
b3.addActionListener(this);
. . .
public void actionPerformed(ActionEvent e) { String command= e.getActionCommand();
if (command==DISABLE) { //They clicked "Disable middle button"
b2.setEnabled(false);
b1.setEnabled(false);
b3.setEnabled(true);
}
if (command == ENABLE) { //They clicked "Enable middle button"
b2.setEnabled(true);
b1.setEnabled(true);
b3.setEnabled(false);
} }
8.6.2 Canvases
The Canvas class exists to be subclassed. It does nothing on its own; it merely provides a way for you to implement a custom Component.
Code Example:
class ImageCanvas extends Canvas { Container pappy;
Image image;
boolean trueSizeKnown = false;
Dimension minSize;
public ImageCanvas(Image image, Container parent, int w, int h) { if (image == null) {
System.err.println("Canvas got invalid image object!");
return;
}
this.image = image;
pappy = parent;
minSize = new Dimension(w,h);
}
public Dimension getPreferredSize() { return getMinimumSize();
}
public Dimension getMinimumSize() { return minSize;
}
public void paint (Graphics g) { if (image != null) {
if (!trueSizeKnown) {
int width= image.getWidth(this);
int height= image.getHeight(this)
if ((imageWidth > 0) && (imageHeight > 0)) { trueSizeKnown = true;
// Component-initiated resizing.
minSize = new Dimension(width,height);
setSize(width, height);
pappy.validate();
} }
g.drawRect(0, 0, minSize.width - 1, minSize.height - 1);
g.drawImage(image, 0, 0, this);
} } }
8.6.3 Checkboxes
Checkboxes are two-state buttons that can be either "on" or "off". When the user clicks a checkbox, the checkbox state changes and it generates an action event.
Code Example:
cb1 = new Checkbox(); //Default state is "off" (false).
cb1.setLabel("Checkbox 1");
cb3 = new Checkbox("Checkbox 3");
cb3.setState(true); //Set state to "on" (true).
cbg = new CheckboxGroup();
cb4 = new Checkbox("Checkbox 4", cbg, false); //initial state: off (false) cb5 = new Checkbox("Checkbox 5", cbg, false); //initial state: off
cb6 = new Checkbox("Checkbox 6", cbg, false); //initial state: off
8.6.4 Choices
The Choice class provides a menu-like list of choices, accessed by a distinctive button. The user presses the button to bring up a "menu", and then chooses one of the items.
Code Example:
choice = new Choice();
choice.addItem("ichi");
choice.addItem("ni");
choice.addItem("san");
choice.addItem("yon");
choice.addItemListener(this);
...
public void itemStateChanged(ItemEvent e) { setLabelText(choice.getSelectedIndex(), choice.getSelectedItem());
}
8.6.5 Lists
The List class provides a scrollable area containing selectable text items.
Code Example:
//Build lists, which allows multiple selections.
spanish = new List(4, true); //prefer 4 items visible spanish.add("uno");
. . .
spanish.add("siete");
spanish.addActionListener(this);
spanish.addItemListener(this);
italian = new List(); //Defaults to none visible, only one selectable italian.add("uno");
. . .
italian.add("sette");
italian.addActionListener(this);
italian.addItemListener(this);
. . .
public void actionPerformed(ActionEvent e) { List list= (List) e.getSource();
String language = (list == spanish) ? "Spanish" : "Italian";
output.append("Action event occurred on \""
+ list.getSelectedItem() + "\" in "
+ language + ".\n");
}
public void itemStateChanged(ItemEvent e) { List list= (List) e.getItemSelectable();
String language = (list == spanish) ? "Spanish" : "Italian";
int index = ((Integer)(e.getItem())).intValue();
if (e.getStateChange() == ItemEvent.SELECTED) { output.append("Select event occurred on item #"
+ index + " (\""
+ list.getItem(index) + "\") in "
+ language + "." + newline);
} else { //the item was deselected
output.append("Deselect event occurred on item #"
+ index + " (\""
+ list.getItem(index) + "\") in "
+ language + "." + newline);
}}
8.6.6 ScrollPanes
A ScrollPane manages a single child component, displaying as much of the component as space permits.
By default, a scroll pane's scrollbars are visible only when they're needed.
Code Example:
ScrollPane sp1 = new ScrollPane();
sp1.add(aComponent);
Scrollbar parameters:
SCROLLBARS_AS_NEEDED
The default value. Show each scrollbar only when it's needed.
SCROLLBARS_ALWAYS Always show scrollbars.
SCROLLBARS_NEVER
Never show scrollbars. You might use this option if you don't want the user to directly control what part of the child component is shown.
Example:
ScrollPane sp2 = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
8.6.7 TextComponent
The TextComponent derived classes, TextArea and TextField, display selectable text and, optionally, allow the user to edit the text.
TextArea and TextField:
From TextComponent this classes inherit methods that allow them to set and get the current selection, enable and disable editing, get the currently selected text (or all the text), and set the text.
Component | TextComponent | +---+
| | TextArea TextField Code Example:
//Where instance variables are defined:
TextField textField;
TextArea textArea;
public void init() {
textField = new TextField(20);
textField.addActionListener(this);
textArea = new TextArea(5, 20);
textArea.setEditable(false);
...//Add the two components to the panel.
}
public void actionPerformed(ActionEvent evt) { String text = textField.getText();
textArea.append(text + "\n");
textField.selectAll();
}
8.6.8 Menus
The following applet shows many of the menu features you're likely to use
Note:
• Menus can exist only in menu bars, and menu bars can be attached only to windows (specifically, to Frames).
• Classes that provide menu functionality do not inherit from Component, since many platforms place severe limits on menu capabilities.
MenuComponent subclasses:
MenuItem
Each item in a menu is represented by a MenuItem object.
CheckboxMenuItem
Each menu item that contains a checkbox is represented by a CheckboxMenuItem object.
Menu
Each menu is represented by a Menu object. Menu is a subclass of MenuItem so that you can create a submenu by adding one menu to another.
Popupmenu
Represents a popup menu.
MenuBar
The MenuBar class represents the platform-dependent notion of a group of menus attached to a window. MenuBars can not be bound to Panels.
MenuContainer Interface:
To be able to contain a MenuComponent, an object must adhere to the MenuContainer interface (Frame, Menu, and MenuBar classes do).
Code Example:
public class MenuWindow extends Frame implements ActionListener, ItemListener {
. . .
public MenuWindow() {
//Build the menu bar.
mb = new MenuBar();
setMenuBar(mb);
//Build first menu in the menu bar.
m1 = new Menu("Menu 1", true);
mb.add(m1);
mi1_1 = new MenuItem("Menu Item 1_1");
m1.add(mi1_1);
. . .
//Build help menu.
m5 = new Menu("Menu 5");
mb.add(m5); //just setting the help menu doesn't work; must add it mb.setHelpMenu(m5);
mi5_1 = new MenuItem("Menu Item 5_1");
mi5_1.setShortcut(new MenuShortcut(KeyEvent.VK_5));
m5.add(mi5_1);
. . .
//Build second menu in the menu bar.
m2 = new Menu("Menu 2");
mb.add(m2);
mi2_1 = new CheckboxMenuItem("Menu Item 2_1");
m2.add(mi2_1);
//Build third menu in the menu bar.
. . .
//Register as an ActionListener for all menu items.
m1.addActionListener(this);
m2.addActionListener(this);
m3.addActionListener(this);
. . .
//Register as ItemListener on checkbox menu item.
mi2_1.addItemListener(this);
} . . .
public void actionPerformed(ActionEvent e) { output.append("\"" + e.getActionCommand() + "\" action detected in menu labeled \""
+ ((MenuItem)(e.getSource())).getLabel() + "\".\n");
}
public void itemStateChanged(ItemEvent e) {
output.append("Item state change detected on item \""
+ e.getItem() + "\" (state is "
+ ((e.getStateChange() ==
ItemEvent.SELECTED)?
"selected)."
: "deselected).") + "\n");
}
8.7 AWT Containers