Hi i'm new to threading and i'm having a difficult time. I have a main program and if a certain event occurs a new dialog pops up. I need the rest of the program to wait for the dialog to finish before it continues. Here's what i have started but it's not working and i'm not sure what to do.
myThread.java
myDialog.java
Reason.java
main.java
i don't want 'finished' to be displayed until the thread is done getting the reason Thanks
myThread.java
Code:
public class myThread extends Thread
{
private myDialog new_reason_;
public myThread()
{
new_reason_ = new myDialog();
}
public void run()
{
System.out.println("running");
String reason = new_reason_.display();
System.out.println("done");
}
Code:
public class myDialog extends JFrame
{
private JButton ok_;
private JTextField reason_name_;
private Thread thread_;
private Reason theReason_;
public myDialog()
{ //set up dialog }
private void registerListeners()
{
ok_.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
theReason_.setReason("Test");
}};
}
public String display()
{
theReason_= new Reason();
this.setVisible(true);
return theReason_.getReason();
}
Code:
public class Reason
{
private String reason_;
public Reason()
{
reason_ = "";
}
public synchronized void setReason(String v)
{
synchronized (this) {
notify();
}
reason_ = v;
}
public synchronized String getReason()
{
try{
System.out.println("waiting...");
wait();
}
catch (InterruptedException e)
{}
System.out.println("done waiting..");
return reason_;
}
}
main.java
Code:
...
myThread thread = new myThread();
thread.start();
JOptionPane.showMessageDialog(null, "finished");
...