Exemplo ListXlet

Esse exemplo exercita o uso das seguintes funcionalidades:

  • Ciclo de vida de Xlet pelo pacote javax.tv.xlet
  • Pintura na tela com Graphics do pacote com.sun.dtv.lwuit..
  • Widigt gráfico LWUIT: List.
import java.util.Vector;

import javax.tv.xlet.Xlet;
import javax.tv.xlet.XletContext;
import javax.tv.xlet.XletStateChangeException;

import com.sun.dtv.lwuit.Form;
import com.sun.dtv.lwuit.List;
import com.sun.dtv.lwuit.events.ActionEvent;
import com.sun.dtv.lwuit.events.ActionListener;
import com.sun.dtv.lwuit.events.SelectionListener;
import com.sun.dtv.lwuit.geom.Dimension;
import com.sun.dtv.lwuit.layouts.BorderLayout;

public class ListXlet extends Form implements Xlet, ActionListener,
        SelectionListener {

    private XletContext context;

    private List list;
    Vector elements;

    public void initXlet(XletContext ctx) throws XletStateChangeException {
        this.context = ctx;
        this.setSize(new Dimension(400, 100));

        setTitle("List");
        elements = new Vector();

        elements.add("Item1");
        elements.add("Item2");
        elements.add("Item3");
        elements.add("Item4");
        elements.add("Item5");
        elements.add("Item6");

        list = new List(elements);
        list.addActionListener(this);
        list.addSelectionListener(this);
        list.setNumericKeyActions(true);

        addComponent(BorderLayout.CENTER, list);
    }

    public void startXlet() throws XletStateChangeException {
        show();
    }

    public void pauseXlet() {
    }

    public void destroyXlet(boolean unconditional)
            throws XletStateChangeException {
        context.notifyDestroyed();
    }

    public void actionPerformed(ActionEvent evt) {
        System.out.println(evt.getSource() + ", " + evt.getKeyEvent() + ", " 
                + evt.getCommand());
        elements.set(list.getSelectedIndex(),
                elements.get(list.getSelectedIndex()) + "(Selecionado)");

    }

    public void selectionChanged(int oldSelected, int newSelected) {
        System.out.println("Old: " + oldSelected + "  New: " + newSelected);
    }

}