Solved

Applet : press any key to continue

Posted on 2003-10-22
6
312 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry, read 'should not' in the last sentence
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count11 challenge 6 47
nestparen challenge 4 55
where is session ID cache stored 1 38
Problem to start Neon 20 48
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now