Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Process keypresses in a while loop

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
Hi,

This is probably an easy question, but how can you get the application to process keypresses if it is in a while loop. In another language called Delphi you can make a call to Application.ProcessMessages in the loop to see if a keypress is made. How do you do this in java?
Code:
Thread t = new Thread(accessOnlineLeaderboard);
t.start();
while (!connectionFinished){}
System.out.println("leaderboard highscores string: " + leaderboardHighscores);

The connectionFinished variable is set to true once an HTTP connection has been completed (within the thread t).

Any help would be much appreciated!


Clive [infinity]
 
Actually, I've refined the code as follows:
Code:
Thread t = new Thread(accessOnlineLeaderboard);
t.start();
// Wait indefinitely for the thread to finish
try 
{
  t.join();
  // Finished
  System.out.println("leaderboard highscores string: " + leaderboardHighscores);
}

The thread assigns a value to leaderboardHighscores. So I want to wait until the thread has finished before displaying the scores. However, an "Is it OK to use airtime" screen appears and I am unable to click "yes" because the application is not processing keypresses while in the join method. So the question still stands!!

Clive [infinity]
 
KeyListener would indeed be a good idea, but anyway :

Code:
public class Test {

	public static void main(String args[]) throws IOException {
		String line = "";
		while (!line.equals("quit")) {
			System.out.println("Enter words, and press return ...");
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			line = br.readLine();
			System.err.println("Entered : " +line);
		}
	}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top