[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Waiting a keypress

Posted on 2004-11-11
7
Medium Priority
?
394 Views
Last Modified: 2012-06-22
I want to create somekind of "Press Any Key" method for my Midlet.

I created a class like this :

class Wait extends Canvas {

    private String message;
    private int done = 0;

    Wait() {
    }

    void start (String msg) {
       done = 0;
       message = msg;
       repaint();
       serviceRepaints();

       while (done == 0) {
       }

    }

    protected void keyPressed(int code) {
       done = 1;
    }

    protected void paint(Graphics g) {
       g.drawString(message, 10, 50, Graphics.LEFT|Graphics.TOP);
    }

}

And I called from my main Midlet like this :

   Wait w = new Wait();
   w.start("Press any key");
   display.setCurrent(w);

But it just locked the process :)

I believe that because of the while loop can not catch the keyPressed. Anyone know any workaround ?
0
Comment
Question by:tambun
5 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 12556404
why not just do a msgbox and they have to press the ok button or pres enter
0
 

Author Comment

by:tambun
ID: 12562951
Could you please show me a code to display msgbox ?
0
 

Author Comment

by:tambun
ID: 12563008
Oh, I am using J2ME / MIDP , not normal java
0
 
LVL 9

Accepted Solution

by:
OBCT earned 240 total points
ID: 12564394
Try using something like this....

synchronized void start (String msg)
{
   done = 0;
   message = msg;
   repaint();
   serviceRepaints();

   while (done == 0)
   {
      try
      {
         wait();
      }
      catch (InterruptedException e) { e.printStackTrace(); }
   }
}

protected synchronized void keyPressed(int code)
{
       done = 1;
       notifyAll();
}

Have a look at this link for more details on dealing with threads.
http://java.sun.com/docs/books/tutorial/essential/threads/index.html

Even though your using J2ME, the thread principals still apply.
0
 
LVL 4

Expert Comment

by:bhpr
ID: 12581620
Hi tambun,

is this OK?

class Wait extends Canvas {

    private String message;
    private int done = 0;

    Wait() {
    }

    void start (String msg) {
       done = 0;
       message = msg;
       repaint();
       serviceRepaints();

       while (done == 0) {
              try { Thread.sleep(100); } catch (Exception _e){ //handle }
       }

    }

    protected void keyPressed(int code) {
       done = 1;
    }

    protected void paint(Graphics g) {
       g.drawString(message, 10, 50, Graphics.LEFT|Graphics.TOP);
    }

}


-bhpr
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we have discussed about the OS X EI Capitan and how to fix Wi-Fi issue in OS X El Capitan. We have explained how to delete system level preferences and create a new Wi-Fi location to resolve Wi-Fi issue.
This paper addresses the security of Sennheiser DECT Contact Center and Office (CC&O) headsets. It describes the DECT security chain comprised of “Pairing”, “Per Call Authentication” and “Encryption”, which are all part of the standard DECT protocol.
This Micro Tutorial will show you how to maximize your wireless card to its maximum capability. This will be demonstrated using Intel(R) Centrino(R) Wireless-N 2230 wireless card on Windows 8 operating system.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question