HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Xlet: A Different Kind of Applet for J2ME"
Vol. 8, Issue 7, p. 54

	



Listing 1 Xlet argument

public void initXlet(XletContext ctx)
        throws XletStateChangeException {
    Container c;
    try {
        c = ctx.getContainer();
    } catch (UnavailableContainerException e) {
        throw new XletStateChangeException(e.getMessage());
    }
    TextField tx = new TextField(30);
    String[] args = (String[]) ctx.getXletProperty(XletContext.ARGS);
    String s = "";
    for (int i = 0; i < args.length; i++) {
        s = s + args[i] + " ";
    }
    tx.setText(s);
    c.setSize(200, 200);
    c.setVisible(true);
    c.add(tx);
}



Listing 2 A simple digital clock

import javax.microedition.xlet.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class ClockXlet implements Xlet, ActionListener {
    
    TextField display;
    MyClock clock;
    Button pauseButton = new Button("Pause");
    Button stopButton = new Button("Stop");
    Button resumeButton = new Button("Resume");
    XletContext context;
    public void initXlet(XletContext ctx)
            throws XletStateChangeException {
        Container c;
        context = ctx;
        try {
            c = ctx.getContainer();
        } catch (UnavailableContainerException e) {
            throw new XletStateChangeException(e.getMessage());
        }
        display = new TextField(30);
        clock = new MyClock(display);
        pauseButton.addActionListener(this);
        resumeButton.addActionListener(this);
        resumeButton.setEnabled(false);
        stopButton.addActionListener(this);
        c.setSize(200, 200);
        c.setVisible(true);
        c.add(display);
        c.add(pauseButton);
        c.add(resumeButton);
        c.add(stopButton);
        clock.start();
    }

    public void startXlet() {
        clock.setPaused(false);
        resumeButton.setEnabled(false);
        pauseButton.setEnabled(true);
    }
 
    public void pauseXlet() {
        clock.setPaused(true);
        resumeButton.setEnabled(true);
        pauseButton.setEnabled(false);
   }

    public void destroyXlet(boolean unconditional) {
        clock.setStopped(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == stopButton) {
            clock.setStopped(true);
            context.notifyDestroyed();
        } else if (e.getSource() == pauseButton) {
            clock.setPaused(true);
            resumeButton.setEnabled(true);
            pauseButton.setEnabled(false);
            context.notifyPaused();
        } else if (e.getSource() == resumeButton) {
            context.resumeRequest();
        }
    }
}

class MyClock extends Thread {

    boolean paused, stopped;
    TextField display;

    public MyClock(TextField t) {
        display = t;
    }

    String getTime() {
        Calendar rightNow = Calendar.getInstance();
        String hour = String.valueOf(rightNow.get(Calendar.HOUR_OF_DAY));
        String min = String.valueOf(rightNow.get(Calendar.MINUTE));
        if (min.length() == 1) {
            min = "0" + min;
        }
        String sec = String.valueOf(rightNow.get(Calendar.SECOND));
        if (sec.length() == 1) {
            sec = "0" + sec;
        }
        return hour + ":" + min + ":" + sec;
    }

    public synchronized boolean isStopped() {
        return stopped;
    }

    public synchronized void setStopped(boolean value) {
        stopped = value;
        notifyAll();
    }

    public synchronized boolean isPaused() {
        return paused;
    }

    public synchronized void setPaused(boolean value) {
        paused = value;
        notifyAll();
    }

    public void run() {
        while (!isStopped()) {
            try {
                if (!isPaused()) {
                    Thread.sleep(1);
                    display.setText(getTime());
                } else {
                    synchronized (this) {
                        wait();
                    }
                }
            } catch (InterruptedException e) {
            }
        }
    }
}

 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.