Skip to main content
LWUIT Tutorial

Event Handling

Responding to Button Clicks

LWUIT notifies your application about events via listener interfaces. Your application provides implementations of the appropriate interface, registers them with LWUIT, and takes action when an event occurs.

To respond to a button press, for example, your application needs to do two things:

  1. Define an implementation of ActionListener.
  2. Pass an instance of the ActionListener to the Button's addActionListener().

The following example uses an inner class to implement ActionListener. Every time you press the button, the label changes.

final Form f = new Form("Events");

Button button = new Button("Push me!");
final Label countLabel = new Label(" ");
button.addActionListener(new ActionListener() {
  private int c = 0;
  public void actionPerformed(ActionEvent ae) {
    c++;
    countLabel.setText(Integer.toString(c));
    f.layoutContainer();
  }
});

The call to layoutContainer() ensures that the label's size is adjusted correctly when the number gets bigger. For example, notice how the size adjusts when the count goes from 9 to 10.

If you use a single listener for more than one button, you can find out which button was pressed by calling the ActionEvent's getSource() method.

Using Commands

LWUIT includes the concept of a command. A command is something that your user can do in your application. LWUIT decides how to show the command on the screen and how the user makes it happen. Usually, commands are assigned to the soft buttons on a mobile phone. LWUIT will normally show commands as labels at the bottom corners of the screen; the user can invoke them by pressing the corresponding soft button. On touch devices, the user can touch directly on a command to invoke it.

Conceptually, commands in LWUIT closely resemble commands in MIDP. Some of the details are slightly different, but if you already understand MIDP commands, you'll have no trouble with LWUIT commands.

In LWUIT, create commands and add them to forms. Then register a listener object with the form to be notified when the user invokes a command.

This simple example creates a form and registers a listener. The listener shuts down the application when it is invoked.

import javax.microedition.midlet.*;

import com.sun.lwuit.*;
import com.sun.lwuit.events.*;

public class HelloLWUITMidlet extends MIDlet implements ActionListener {

  private Form mForm;

  public void startApp() {
    if (mForm == null) {
      Display.init(this);

      Form f = new Form("Hello, LWUIT!");

      Command exitCommand = new Command("Exit");
      f.addCommand(exitCommand);
      f.setCommandListener(this);

      f.show();
    }
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void actionPerformed(ActionEvent ae) {
    destroyApp(true);
    notifyDestroyed();
  }
}

If you add more than two commands to a form, they won't fit on the two available soft buttons. In this case, one of the soft buttons will become a menu. When the user invokes the menu, it shows a list of the rest of the commands.

When your listener's actionPerformed() method must respond to more than one command, you can find out which one was invoked by calling the ActionEvent's getCommand() method.

Other Event Types

Most of your event handling will involve ActionListener for commands and buttons, but LWUIT includes other useful listeners:

  • Use DataChangedListener to find out when a ListModel changes. Remember, ListModel represents the data behind a list or combo box.
  • A SelectionListener is notified when the current selection of a ListModel changes.
  • A FocusListener is notified when a component gains or loses focus.
  • Finally, StyleListener receives notifications about changes in a component's style. The next section of this tutorial describes styles and themes.

Copyright © 2008 Sun Microsystems, Inc.
 
 
Close
loading
Please Confirm
Close