I'm looking to create a method somewhat like JOptionPane's static showXxxDialog methods. Their JavaDoc says that "Each showXxxDialog method blocks the current thread until the user's interaction is complete." So I was trying to block the thread, and getting java.lang.IllegalMonitorStateException: current thread not owner exceptions. Any suggestions? Here's a stripped down version of the code...
import ...;
public class ObjectMaker extends JPanel {
private Object obj = null;
class NewObjectListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
synchronized (ObjectMaker.this) {
ObjectMaker.this.notifyAll();
}
}
}
public static Object makeObject(Component parentComponent) {
ObjectMaker newObjectPanel = new ObjectMaker();
Component parent = parentComponent;
while (parent != null && !(parent instanceof Frame))
parent = parent.getParent();
JDialog newObjectDialog = new JDialog((parent != null) ? (Frame) parent : null, "Add Object", true);
newObjectDialog.setContentPane(newObjectPanel);
newObjectDialog.setSize(300, 230);
newObjectDialog.show();
synchronized (newObjectPanel) {
try {
parent.wait();
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
}
if (null == newObjectPanel.obj)
throw new IllegalStateException("null == newObjectPanel");
newObjectDialog.dispose();
return newObjectPanel.obj;
}
public ObjectMaker() {
obj = "...";
}
}
Liam Morley
lmorley@wpi.edu
"light the deep, and bring silence to the world.
light the world, and bring depth to the silence."
import ...;
public class ObjectMaker extends JPanel {
private Object obj = null;
class NewObjectListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
synchronized (ObjectMaker.this) {
ObjectMaker.this.notifyAll();
}
}
}
public static Object makeObject(Component parentComponent) {
ObjectMaker newObjectPanel = new ObjectMaker();
Component parent = parentComponent;
while (parent != null && !(parent instanceof Frame))
parent = parent.getParent();
JDialog newObjectDialog = new JDialog((parent != null) ? (Frame) parent : null, "Add Object", true);
newObjectDialog.setContentPane(newObjectPanel);
newObjectDialog.setSize(300, 230);
newObjectDialog.show();
synchronized (newObjectPanel) {
try {
parent.wait();
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
}
if (null == newObjectPanel.obj)
throw new IllegalStateException("null == newObjectPanel");
newObjectDialog.dispose();
return newObjectPanel.obj;
}
public ObjectMaker() {
obj = "...";
}
}
Liam Morley
lmorley@wpi.edu
"light the deep, and bring silence to the world.
light the world, and bring depth to the silence."