3 Replies - 4244 Views - Last Post: 14 April 2009 - 06:01 PM Rate Topic: -----

#1 castrike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-January 07

Threads and JOptionPane

Posted 12 April 2009 - 02:03 PM

Hey everyone, I am trying to write a program which got to get some input from the user and its timed... Let's say, I ask the user a question and that user has only 10 seconds to respond.
My program has 2 threads, a turn thread and a timer thread. When the player enters some input the timer thread stops and if the user doesn't reply in 10 seconds, the user thread gets stopped.
The problem comes in the way i get the input from the user. So far im using JOptionPane and when the timer kills the turn thread the panel remains. Any ideas of how to remove it?

I tried creating a JOptionPane object, initializing it in Turn's run method.


Im sort of new to threads and guis with java. Any help/ideas are appreciated.

class Turn extends Thread {
	private boolean done=false;
	public void run() {	   
		Main.ans = JOptionPane.showOptionDialog(Main.test, "Is the celebrity's answer correct?", "word", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
		
		if(!done) {
			Main.terminateTimer();
			System.out.println("Timer got killed");
			System.out.println("ANS: "+Main.ans);
		}
	}

	public void kill() {
		done=true;
	}
}

class TimerThread extends Thread {
	private boolean done=false;
	public static long startTime = System.currentTimeMillis();

	protected static final long age() {
		return (System.currentTimeMillis() - startTime);
	}
	
	public void run() {
		System.out.println(this.getName());
		while(age()<1000 && !done) {;} //wait 1 second
		if(!done) {
			Main.terminateTurn(); // terminate players turn
			System.out.println("TIMES UP!");
			System.out.println("ANS: "+Main.ans);
		}
	}

	public void kill() {
		done = true;
	}
}

public class Main {
	static int ans=-1;
	static Turn turn = new Turn();
	static TimerThread timer = new TimerThread();
	public static void terminateTurn() {
		turn.kill();
	}

	public static void terminateTimer() {
		timer.kill();
	}

	public static void main(String[] args) {
		turn.start();
		timer.start();
	   
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Threads and JOptionPane

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Threads and JOptionPane

Posted 12 April 2009 - 09:09 PM

Ouf... how complicated for nothing....

And Swing is NOT thread safe so your architecture has many flaws

So:

share String answer and JOptionPane op


In your main class:

create you JOptionPane op and initialize answer to an impossible value like: "PBL is always right"

fire a thread that simply: answer = op.showInput....

then loop for the time you want 10 seconds: 10 times 1 second

for(int i = 0; i < 10; i++) {
	try {
		Thread.sleep(1000L);
	}
	catch(Exception e) {}

	if(answer.equals("PBL is always right"))  {	 // nothing happened
	}
	else {		// user input something
	   break;
	}
}
// ok out of loop make sure optionPane not visible anymore
// or because it was answered or because timer expired
op.setVisible(false);


Was This Post Helpful? 0
  • +
  • -

#3 castrike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-January 07

Re: Threads and JOptionPane

Posted 14 April 2009 - 12:15 PM

Thank you so much. Your reply was of great help. I actually did a bit more research and came up with something like this and it's working perfectly except that i have to work on the casting in the run method. Here is my code and i hope i can be of any help if anyone else encounters something similar.
class TurnThread extends Thread {
	private boolean done=false;
	public static long startTime = System.currentTimeMillis();

	protected static final long age() {
		return (System.currentTimeMillis() - startTime);
	}
	
	public void run() {
		Main.dialog.setVisible(true);
		Main.answer = (Integer)Main.popup.getValue(); //this is where i have to do some work now
	}
}

public class Main {
	public static int answer=-1;
	public static JOptionPane popup = new JOptionPane("Some Question?",JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
	public static JDialog dialog = popup.createDialog(null,"Question");
	public static void main(String[] args) {
		TurnThread turn = new TurnThread();
		boolean userInput = false;
		turn.start();
		for(int i=0; i<5; i++) { // loop runs for 5 seconds
			try { Thread.sleep(1000); } catch(Exception e) {} // wait a second
			if(answer!=-1) { userInput=true; break;}
		}
		if(!userInput) {
			System.out.println("user didnt answer");
			dialog.setVisible(false);
		} else {
			System.out.println("user answered!!!:: "+answer);
		}
	}

}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Threads and JOptionPane

Posted 14 April 2009 - 06:01 PM

Glad I helped
Do not see why you have to JOptionPane.getValue();

use the String returned
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1