What I'm trying to do:
Launch a window to request parameters
What I've tried:
vp.wait()
, the window disappears.notify()
, the program doesn`t wait.Here's my code:
public static void main(String[] args) throws InterruptedException{
if(args.length==0){
ParamsWind vp = new ParamsWind();
vp.setVisible(true);
synchronized (vp){
try {
vp.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
.....
public class ParamsWind extends JDialog {
...
public ParamsWind()
....
//Create Ok Button and program Action Listener
Button ok = new Button("OK");
...
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(f.getText().equals("") || r.getText().equals("")){
ErrorWind verr = new ErrorWind();
verr.setVisible(true);
}
notify();
Well here's the problem, in actionPerformed
.
notify();
You're doing that without a synchronized
block, so the thread doesn't own the monitor for this
... hence the exception.
However, you don't just want a synchronized
block, because you're actually calling notify()
on the wrong object. You want to use the ParamsWind
:
synchronized(ParamsWind.this) {
ParamsWind.this.notify();
}
It's not clear to me that using wait()
and notify()
is really what you want here - or that you won't end up with a race condition - but those are the immediate problems with what you're doing.
See more on this question at Stackoverflow