Solved

Applet : press any key to continue

Posted on 2003-10-22
6
336 Views
Last Modified: 2010-03-31
Hi experts,
I have written an applet that processes some datas, however, at one point, I need the user to press any key (or the enter key) to continue.
This is keyevent is not linked to any object like a button. I tried something like :
this.addKeyListener(new KeyAdapter() {
  public void KeyPressed(KeyEvent e) {
  }
  });
but this produce an extra file$1.class that bothers me (it needs to become a signed jar).
I tried playing with something like : if ( ev.id == Event.KEY_PRESS ) with no success.
I'm sure there's a simple way to write in an applet something similar to : while ((ch = (char)System.in.read()) !=...).
Thanks experts for your answer !

0
Comment
Question by:everfly
6 Comments
 
LVL 3

Accepted Solution

by:
savalou earned 20 total points
ID: 9599148
Why can't you include the file$1.class in your signed jar?

The file$1.class is generated because you're using an anonymous subclass of KeyAdaptor (new KeyAdaptor() {//this is an anonymous class...}).  To get around this you can write your own class like MyKeyAdaptor.java in which you extend KeyAdaptor.
0
 

Author Comment

by:everfly
ID: 9599535
Thanks savalou. Right, I included the file$1.class in my signed jar but adding "this.addKeyListener(new KeyAdapter() {public void KeyPressed(KeyEvent e) {}});" doesn't wait for me to press any key.
And also, isn't there a easier way to perfom this ?
Thanks.
0
 
LVL 92

Expert Comment

by:objects
ID: 10194156
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept savalou's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

objects
EE Cleanup Volunteer
0
 

Expert Comment

by:Bobby_Hirst
ID: 10220786
How about this:

this.addKeyListener(new KeyAdapter() {
    public void KeyPressed(KeyEvent e) {
        continue=true;
    }
});"

//add this bit where you want execution to stop:
boolean continue=false;
while (!continue) {}  //this will loop, until the KeyEvent allows the
                              //execution to continue.

I think that should solve it... The thing that you hadn't taken into account is that KeyListener runs in parallel with your code listening for Events, and if no event is assigned to it, nothing will happen to your main code's execution.  As for the extra class file, I think it's unavoidable but should cause any problems when you create a signed jar
0
 

Expert Comment

by:Bobby_Hirst
ID: 10220797
Sorry, read 'should not' in the last sentence
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

777 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