Trinithis
06-29-2007, 09:41 PM
How do I access the thread that is created by a swing window, such as an extension of JFrame, from within the thread that created the window?
For example, I have a method that does something like this that is executed from the main thread. I want to join() the frameThread with the currentThread:
//Part of a method that has been called
Thread currentThread = Thread.currentThread();
//Extraneous code here
MyFrame frame = new MyFrame();
//A new thread is created behind the scenes from the MyFrame object, which extends JFrame.
Thread frameThread = .......?
frameThread.join();
return frame.calculatedValue;
What I'm coding is something like JOptionPane.showInputDialog(), but instead of that, I coded a InputForm.createFormDialog(), which brings up a window with multipule text fields and returns a String array when submitted. Here's the static method:
public static String[] createFormDialog(final String[] labelTexts) {
Thread thread = Thread.currentThread();
InputForm form = new InputForm(thread, labelTexts); //InputForm only has private constructors.
form.sleepCallingThread();
return form.returnString;
}
Because InputForm extends JFrame, it creates its own thread, and if I don't pause the thread that called the InputForm.createFormDialog(), then createFormDialog() will happily continue and return form.returnString, which happens to be null before a user submits his form.
My current solution involves sleeping the current thread while a certain bool is false. Then when the user clicks a submit button, the bool turns true and the currentThread is inturruped, whereupon the form.returnString is returned.
private void sleepCallingThread() {
while(!formSubmitted) {
try {
callingThread.sleep(3600000);
}
catch(InterruptedException e) {
return;
}
}
}
Even though this works, I'm not happy with my technique. I've tried synchronized/wait()/notify(), but as I don't know threads/monitors very well, I couldn't get it to work. So my next idea is to do something like: frameThread.join(), but my dilemma is how to even get access to the frameThread in the first place.
If you want to see my source code in txt:
http://trinithis.awardspace.com/InputForm/InputForm.txt
http://trinithis.awardspace.com/InputForm/TestInputForm.txt
For example, I have a method that does something like this that is executed from the main thread. I want to join() the frameThread with the currentThread:
//Part of a method that has been called
Thread currentThread = Thread.currentThread();
//Extraneous code here
MyFrame frame = new MyFrame();
//A new thread is created behind the scenes from the MyFrame object, which extends JFrame.
Thread frameThread = .......?
frameThread.join();
return frame.calculatedValue;
What I'm coding is something like JOptionPane.showInputDialog(), but instead of that, I coded a InputForm.createFormDialog(), which brings up a window with multipule text fields and returns a String array when submitted. Here's the static method:
public static String[] createFormDialog(final String[] labelTexts) {
Thread thread = Thread.currentThread();
InputForm form = new InputForm(thread, labelTexts); //InputForm only has private constructors.
form.sleepCallingThread();
return form.returnString;
}
Because InputForm extends JFrame, it creates its own thread, and if I don't pause the thread that called the InputForm.createFormDialog(), then createFormDialog() will happily continue and return form.returnString, which happens to be null before a user submits his form.
My current solution involves sleeping the current thread while a certain bool is false. Then when the user clicks a submit button, the bool turns true and the currentThread is inturruped, whereupon the form.returnString is returned.
private void sleepCallingThread() {
while(!formSubmitted) {
try {
callingThread.sleep(3600000);
}
catch(InterruptedException e) {
return;
}
}
}
Even though this works, I'm not happy with my technique. I've tried synchronized/wait()/notify(), but as I don't know threads/monitors very well, I couldn't get it to work. So my next idea is to do something like: frameThread.join(), but my dilemma is how to even get access to the frameThread in the first place.
If you want to see my source code in txt:
http://trinithis.awardspace.com/InputForm/InputForm.txt
http://trinithis.awardspace.com/InputForm/TestInputForm.txt