Java Reference
In-Depth Information
When using this approach, the class should implement the Compo-
nentListener interface, which allows your code to handle events
raised by key presses, item selection events, and so on. Softkey events
are handled slightly differently by implementing the SoftKeyListener
interface. In this case, the MainView class acts as its own listener for
both types of event, keeping all code localized as much as possible:
private class MainView extends Panel
implements ComponentListener, SoftKeyListener {
private ListBox listBox;
public MainView() {
setTitle(APP_TITLE);
// populate list from database
int count = database.size();
listBox = new ListBox(ListBox.SINGLE_SELECT);
listBox.append("Add New");
for(int i = 0; i < count; i++) {
listBox.append( ((WeightEntry) database.elementAt(i)).toString());
} listBox.select(0);
add(listBox);
// set self as listener
setSoftLabel(Frame.SOFT_KEY_1, "Graph");
setSoftLabel(Frame.SOFT_KEY_2, "Exit");
setSoftKeyListener(this);
setComponentListener(this);
}
// from SoftKeyListener
public void softKeyPressed(int softKey) {}
public void softKeyReleased(int softKey)
{
try {
if(softKey == Frame.SOFT_KEY_1) { // left SK
if(database.size() == 0)
throw new Exception("No data to graph");
SwitchView(VIEW_GRAPH_DATA);
} else
IApplication.getCurrentApp().terminate();// right SK : shutdown
} catch(Exception e) {
showError(e.getMessage());
}
} // from ComponentListener
public void componentAction(Component source, int type, int param) {
if(source == listBox) {
int index = listBox.getSelectedIndex();
if(index == 0)
currentEntry = new WeightEntry();// add new
else // edit existing
 
Search WWH ::




Custom Search