Skip to main content

sleep

8 replies [Last post]
heziflash
Offline
Joined: 2008-06-03
Points: 0

Hi

I'm trying to run this code;

import java.util.TimerTask;
import java.util.Timer;

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

public class Learn1 extends Form {
TextField noOfCigTxt;
private int _LearnHowManyCig=0;

Timer timer;
MyTimerTask timertask;

private Form frmMain;

private Operation opr;

public Learn1() {
super("Learn");

set_Command_listener_learn1();

}

public void set_Command_listener_learn1()
{
frmMain = new Form("Learn");

Label name = new Label("Time ");
frmMain.addComponent(name);
noOfCigTxt = new TextField();
frmMain.addComponent( noOfCigTxt);

frmMain.show();

timer = new Timer();
timertask = new MyTimerTask();
timer.schedule(timertask,1,1);

frmMain.addCommand(new Command("OK") {

public void actionPerformed(ActionEvent arg0) {

System.out.println("OK333 selected");
_LearnHowManyCig++;
System.out.println(_LearnHowManyCig);
}
});
//frmMain.show();

try{
Thread.sleep(10000);
}catch(InterruptedException i){}
timer.cancel();

opr = new Operation("opr", this);
opr.set_Command_listener_opr();
}

private class MyTimerTask extends TimerTask {
public void run() {
frmMain.show();
noOfCigTxt.setText( Calendar.getInstance().get(Calendar.MINUTE)+":"+Calendar.getInstance().get(Calendar.SECOND)
+"\n"+"no of cig "+_LearnHowManyCig);

}
}
What i want is to show the frmMain to show immediatly on the screen, and time is incrementig evrey second and no of cig incrementing each time i push the "OK" button.
insted, i'm getting only after 10 sec (sleep(10000)) the form.

What is wrong?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Qunhuan Mei

Hi,

Suppose I have got an over sized image, with its width and height being larger than screen’s, and would like to display it and "CENTER" aligned.

Seems to me using LWUIT, the only way is to create an image Label and add it to a form. I tried using imageLabel.setAlignment(Label.CENTER) to align the image but seems to have no positioning effect. The display always align the image at top_left.

Any idea to position the image in the center using existing fucntions?

Thanks,

Qunhuan

Shai Almog

Hi Qunhuan,
the only reasonable way I can think of for doing this is through
building your own label that paints itself. Center is mostly designed
for other purposes and the different modes are complex enough as it is.

Thanks,
Shai.

On Jun 27, 2008, at 3:48 PM, Qunhuan Mei wrote:

> Hi,
>
> Suppose I have got an over sized image, with its width and height
> being larger than screen’s, and would like to display it and
> "CENTER" aligned.
>
> Seems to me using LWUIT, the only way is to create an image Label
> and add it to a form. I tried using imageLabel.setAlignment
> (Label.CENTER) to align the image but seems to have no positioning
> effect. The display always align the image at top_left.
>
> Any idea to position the image in the center using existing fucntions?
>
> Thanks,
>
> Qunhuan

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

Shai Almog

Hi Hezi,
there is little difference between looping and sleeping when it comes
to this issue. Use asynchronous code code rather than sleep and never
hold the event dispatch thread.
I highly doubt this code worked in MIDP lcdui as it appears here
without such a delay since you are blocking the constructor from
executing.

Shai.

> Hi Shai
>
> I tryed to writ a program based on Date.getTim() and it didnot help.
> Becouse i'm new in java (only a month), i need help how to write
> application in event driven mechanisem.( It was very easy in
> embbedd systems with C. You just setup a timer interrupt and when
> time passed u recieve an inetrrupt).
> P.S the application worked (in j2me) when i didnot use LWUIT , I
> only use Alert and sleep.
> Here is the code:
> public class Learn1 extends Form {
> TextField noOfCigTxt,timeTxt;
> private int _LearnHowManyCig=0;
>
>
> private Form frmMain;
>
> private Operation opr;
>
> public Learn1() {
> super("Learn");
>
> set_Command_listener_learn1();
>
> }
>
> public int learnGetNo0fCig()
> {
> return _LearnHowManyCig;
> }
>
> public void set_Command_listener_learn1()
> {
> frmMain = new Form("Learn");
>
> Label name = new Label("Time ");
> frmMain.addComponent(name);
> timeTxt = new TextField();
> frmMain.addComponent( timeTxt);
>
> Label cig = new Label("\nno of cig ");
> frmMain.addComponent(cig);
> noOfCigTxt = new TextField();
> frmMain.addComponent( noOfCigTxt);
>
>
> Date now = new Date();
> Date forw = new Date();
> long startTime = now.getTime();
> long than=forw.getTime();;
> int count = 0;
>
>
> while (count < 20) {
>
> while ( than >
>
> for (int i=0;i<40000;i++){
> int a =5;
> a ++;
> }
> forw = new Date();
> than = forw.getTime();
> }
>
> count++;
> now = new Date();
> startTime = now.getTime();
> startTime+=1000;
>
> timeTxt.setText( Calendar.getInstance().get
> (Calendar.MINUTE)+":"+Calendar.getInstance().get(Calendar.SECOND));
> noOfCigTxt.setText( " "+_LearnHowManyCig);
>
> frmMain.addCommand(new Command("OK") {
>
> public void actionPerformed(ActionEvent arg0) {
> ");
> _LearnHowManyCig++;
> }
> });
>
>
> frmMain.show();
>
> }
> }
>
> public int getHowManyCic()
> {
>
> return _LearnHowManyCig;
> }
>
> }
>
> Thanks
> Hezi
> [Message sent by forum member 'heziflash' (heziflash)]
>
> http://forums.java.net/jive/thread.jspa?messageID=282923
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

heziflash
Offline
Joined: 2008-06-03
Points: 0

Hi Shai
I'll appricate if you can help me and be more specific with ashyncrounus code. Can u give me the code?

u can try this code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Calendar;
import java.util.TimerTask;
import java.util.Timer;

public class Learn1 extends Form implements CommandListener {

private int _LearnHowManyCig=0;
HelloMidlet _midlet;
Timer timer;
MyTimerTask timertask;
private StringItem msg;
private Form frmMain;
private Command openCommand ;
private Operation opr;
public Learn1(String title, HelloMidlet midlet) {

super(title);

_midlet = midlet;

}
public void commandAction(Command c, Displayable d) {
System.out.println("Saw the command: "+c);
_LearnHowManyCig++;
}

public int learnGetNo0fCig()
{
return _LearnHowManyCig;
}

public void learnSetNo0fCig(int no)
{
_LearnHowManyCig+=no;
}

public void set_Command_listener_learn1()
{
frmMain = new Form("Learn");
msg = new StringItem("Time","Timer not started.");
frmMain.append(msg);
timer = new Timer();
timertask = new MyTimerTask();
timer.schedule(timertask,1,1);
msg.setText("Timer Started");
frmMain .addCommand (get_openCommand ());

frmMain .setCommandListener (this);
try{
Thread.sleep(20000);
}catch(InterruptedException i){}
timer.cancel();
opr = new Operation("opr", _midlet,this);
opr.set_Command_listener_opr();
}

public Command get_openCommand ()
{
if (openCommand == null)
{//GEN-END:MVDGetBegin12
// Insert pre-init code here
openCommand = new Command ("Open", Command.OK, 1);//GEN-LINE:MVDGetInit12
// Insert post-init code here
}//GEN-BEGIN:MVDGetEnd12
return openCommand;
}

public int getHowManyCic()
{

return _LearnHowManyCig;
}
private class MyTimerTask extends TimerTask {
public void run() {

msg.setText( Calendar.getInstance().get(Calendar.MINUTE)+":"+Calendar.getInstance().get(Calendar.SECOND)
+"\n"+"no of cig "+_LearnHowManyCig);
_midlet.getDisplay ().setCurrent (frmMain );

}

}

}

Shai Almog

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Calendar;
import java.util.TimerTask;
import java.util.Timer;

public class Learn1 extends Form implements CommandListener {

private int _LearnHowManyCig=0;
HelloMidlet _midlet;
Timer timer;
MyTimerTask timertask;
private StringItem msg;
private Form frmMain;
private Command openCommand ;
private Operation opr;
public Learn1(String title, HelloMidlet midlet) {

super(title);

_midlet = midlet;

}
public void commandAction(Command c, Displayable d) {
System.out.println("Saw the command: "+c);
_LearnHowManyCig++;
}

public int learnGetNo0fCig()
{
return _LearnHowManyCig;
}

public void learnSetNo0fCig(int no)
{
_LearnHowManyCig+=no;
}

public void set_Command_listener_learn1()
{
frmMain = new Form("Learn");
msg = new StringItem("Time","Timer not started.");
frmMain.append(msg);
timer = new Timer();
timertask = new MyTimerTask();
timer.schedule(timertask,1,1);
msg.setText("Timer Started");
frmMain .addCommand (get_openCommand ());

frmMain .setCommandListener (this);
}

public Command get_openCommand ()
{
if (openCommand == null)
{//GEN-END:MVDGetBegin12
// Insert pre-init code here
openCommand = new Command ("Open", Command.OK, 1);//GEN-
LINE:MVDGetInit12
// Insert post-init code here
}//GEN-BEGIN:MVDGetEnd12
return openCommand;
}

public int getHowManyCic()
{

return _LearnHowManyCig;
}
private class MyTimerTask extends TimerTask {
private long startTime = System.currentTimeMillis();
public void run() {

msg.setText( Calendar.getInstance().get
(Calendar.MINUTE)+":"+Calendar.getInstance().get(Calendar.SECOND)
+"\n"+"no of cig "+_LearnHowManyCig);
_midlet.getDisplay ().setCurrent (frmMain );

if(System.currentTimeMillis() - startTime > 20000) {
timer.cancel();
opr = new Operation("opr", _midlet,this);
opr.set_Command_listener_opr();
}

}

}

}

> Hi Shai
> I'll appricate if you can help me and be more specific with
> ashyncrounus code. Can u give me the code?
>
> u can try this code:
> import javax.microedition.midlet.*;
> import javax.microedition.lcdui.*;
> import java.util.Calendar;
> import java.util.TimerTask;
> import java.util.Timer;
>
> public class Learn1 extends Form implements CommandListener {
>
> private int _LearnHowManyCig=0;
> HelloMidlet _midlet;
> Timer timer;
> MyTimerTask timertask;
> private StringItem msg;
> private Form frmMain;
> private Command openCommand ;
> private Operation opr;
> public Learn1(String title, HelloMidlet midlet) {
>
> super(title);
>
> _midlet = midlet;
>
> }
> public void commandAction(Command c, Displayable d) {
> System.out.println("Saw the command: "+c);
> _LearnHowManyCig++;
> }
>
> public int learnGetNo0fCig()
> {
> return _LearnHowManyCig;
> }
>
> public void learnSetNo0fCig(int no)
> {
> _LearnHowManyCig+=no;
> }
>
> public void set_Command_listener_learn1()
> {
> frmMain = new Form("Learn");
> msg = new StringItem("Time","Timer not started.");
> frmMain.append(msg);
> timer = new Timer();
> timertask = new MyTimerTask();
> timer.schedule(timertask,1,1);
> msg.setText("Timer Started");
> frmMain .addCommand (get_openCommand ());
>
> frmMain .setCommandListener (this);
> try{
> Thread.sleep(20000);
> }catch(InterruptedException i){}
> timer.cancel();
> opr = new Operation("opr", _midlet,this);
> opr.set_Command_listener_opr();
> }
>
> public Command get_openCommand ()
> {
> if (openCommand == null)
> {//GEN-END:MVDGetBegin12
> // Insert pre-init code here
> openCommand = new Command ("Open", Command.OK, 1);//GEN-
> LINE:MVDGetInit12
> // Insert post-init code here
> }//GEN-BEGIN:MVDGetEnd12
> return openCommand;
> }
>
> public int getHowManyCic()
> {
>
> return _LearnHowManyCig;
> }
> private class MyTimerTask extends TimerTask {
> public void run() {
>
> msg.setText( Calendar.getInstance().get
> (Calendar.MINUTE)+":"+Calendar.getInstance().get(Calendar.SECOND)
> +"\n"+"no of cig "+_LearnHowManyCig);
> _midlet.getDisplay ().setCurrent (frmMain );
>
>
> }
>
>
>
>
>
> }
>
> }
> [Message sent by forum member 'heziflash' (heziflash)]
>
> http://forums.java.net/jive/thread.jspa?messageID=282993
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

heziflash
Offline
Joined: 2008-06-03
Points: 0

Hi Shai

I tryed to writ a program based on Date.getTim() and it didnot help.
Becouse i'm new in java (only a month), i need help how to write application in event driven mechanisem.( It was very easy in embbedd systems with C. You just setup a timer interrupt and when time passed u recieve an inetrrupt).
P.S the application worked (in j2me) when i didnot use LWUIT , I only use Alert and sleep.
Here is the code:
public class Learn1 extends Form {
TextField noOfCigTxt,timeTxt;
private int _LearnHowManyCig=0;

private Form frmMain;

private Operation opr;

public Learn1() {
super("Learn");

set_Command_listener_learn1();

}

public int learnGetNo0fCig()
{
return _LearnHowManyCig;
}

public void set_Command_listener_learn1()
{
frmMain = new Form("Learn");

Label name = new Label("Time ");
frmMain.addComponent(name);
timeTxt = new TextField();
frmMain.addComponent( timeTxt);

Label cig = new Label("\nno of cig ");
frmMain.addComponent(cig);
noOfCigTxt = new TextField();
frmMain.addComponent( noOfCigTxt);

Date now = new Date();
Date forw = new Date();
long startTime = now.getTime();
long than=forw.getTime();;
int count = 0;

while (count < 20) {

while ( than

for (int i=0;i<40000;i++){
int a =5;
a ++;
}
forw = new Date();
than = forw.getTime();
}

count++;
now = new Date();
startTime = now.getTime();
startTime+=1000;

timeTxt.setText( Calendar.getInstance().get(Calendar.MINUTE)+":"+Calendar.getInstance().get(Calendar.SECOND));
noOfCigTxt.setText( " "+_LearnHowManyCig);

frmMain.addCommand(new Command("OK") {

public void actionPerformed(ActionEvent arg0) {
");
_LearnHowManyCig++;
}
});

frmMain.show();

}
}

public int getHowManyCic()
{

return _LearnHowManyCig;
}

}

Thanks
Hezi

Francis De Brabandere

just a remark:
there is no need to create a Date to get the timestamp, just call
System.currentTimeMillis();

On Fri, Jun 27, 2008 at 11:06 AM, wrote:
> Hi Shai
>
> I tryed to writ a program based on Date.getTim() and it didnot help.
> Becouse i'm new in java (only a month), i need help how to write application in event driven mechanisem.( It was very easy in embbedd systems with C. You just setup a timer interrupt and when time passed u recieve an inetrrupt).
> P.S the application worked (in j2me) when i didnot use LWUIT , I only use Alert and sleep.
> Here is the code:
> public class Learn1 extends Form {
> TextField noOfCigTxt,timeTxt;
> private int _LearnHowManyCig=0;
>
>
> private Form frmMain;
>
> private Operation opr;
>
> public Learn1() {
> super("Learn");
>
> set_Command_listener_learn1();
>
> }
>
> public int learnGetNo0fCig()
> {
> return _LearnHowManyCig;
> }
>
> public void set_Command_listener_learn1()
> {
> frmMain = new Form("Learn");
>
> Label name = new Label("Time ");
> frmMain.addComponent(name);
> timeTxt = new TextField();
> frmMain.addComponent( timeTxt);
>
> Label cig = new Label("\nno of cig ");
> frmMain.addComponent(cig);
> noOfCigTxt = new TextField();
> frmMain.addComponent( noOfCigTxt);
>
>
> Date now = new Date();
> Date forw = new Date();
> long startTime = now.getTime();
> long than=forw.getTime();;
> int count = 0;
>
>
> while (count < 20) {
>
> while ( than >
>
> for (int i=0;i<40000;i++){
> int a =5;
> a ++;
> }
> forw = new Date();
> than = forw.getTime();
> }
>
> count++;
> now = new Date();
> startTime = now.getTime();
> startTime+=1000;
>
> timeTxt.setText( Calendar.getInstance().get(Calendar.MINUTE)+":"+Calendar.getInstance().get(Calendar.SECOND));
> noOfCigTxt.setText( " "+_LearnHowManyCig);
>
> frmMain.addCommand(new Command("OK") {
>
> public void actionPerformed(ActionEvent arg0) {
> ");
> _LearnHowManyCig++;
> }
> });
>
>
> frmMain.show();
>
> }
> }
>
> public int getHowManyCic()
> {
>
> return _LearnHowManyCig;
> }
>
> }
>
> Thanks
> Hezi
> [Message sent by forum member 'heziflash' (heziflash)]
>
> http://forums.java.net/jive/thread.jspa?messageID=282923
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>

--
http://www.somatik.be
Microsoft gives you windows, Linux gives you the whole house.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

Shai Almog

Hi Hezi,
You are sleeping on the MIDP thread blocking it from painting the
screen. You cannot block the MIDP thread or the EDT and must design
your applications as event driven rather than use sleep (unless you
launch a separate thread which is a whole other complexity).

Thanks,
Shai.

> try{
> Thread.sleep(10000);
> }catch(InterruptedException i){}
> timer.cancel();
>
> opr = new Operation("opr", this);
> opr.set_Command_listener_opr();
> }
>
>
> private class MyTimerTask extends TimerTask {
> public void run() {
> frmMain.show();
> noOfCigTxt.setText( Calendar.getInstance().get
> (Calendar.MINUTE)+":"+Calendar.getInstance().get(Calendar.SECOND)
> +"\n"+"no of cig "+_LearnHowManyCig);
>
>
>
> }
> }
> What i want is to show the frmMain to show immediatly on the
> screen, and time is incrementig evrey second and no of cig
> incrementing each time i push the "OK" button.
> insted, i'm getting only after 10 sec (sleep(10000)) the form.
>
> What is wrong?
> [Message sent by forum member 'heziflash' (heziflash)]
>
> http://forums.java.net/jive/thread.jspa?messageID=282256
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net