Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

blocking a Swing thread, returning an object from a static gui method

Status
Not open for further replies.

carpeliam

Programmer
Mar 17, 2000
990
US
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 java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFrame extends Frame implements Runnable
{
Thread thread;
static volatile boolean runTask = true;
static Communicate communicateObj = new Communicate();
public MyFrame()
{
setLayout(new BorderLayout());
Label lb = new Label("hello");
add(lb,BorderLayout.CENTER);
thread = new Thread(this);
}
public static void main(String args[])
{
MyFrame myframe = new MyFrame();
myframe.setSize(700,500);
myframe.setVisible(true);
(myframe.thread).start();
MyObject myObject = new MyObject(myframe, communicateObj);
}
public void run()
{
while (runTask)
{
if (!communicateObj.getParentWait())
System.out.println(System.currentTimeMillis());
System.out.println("runTask");
}
}
}
class Communicate
{
private static boolean parentWait = false;
private static Object objectForWait;
private static Object objectForWait2;
public Communicate()
{
objectForWait = new Object();
}
public synchronized void releaseLock()
{
parentWait = false;
System.out.println("no wait la");
notifyAll();
}
public boolean getParentWait()
{
if (parentWait==true)
{
System.out.println("getParentWait");
synchronized(this)
{
try
{
System.out.println(parentWait);
wait();
}
catch (InterruptedException ie)
{System.out.println("interrupted");
}
}
}
return false;
}
public void setParentWait()
{
parentWait = true;
}
}
class MyObject extends Panel implements ActionListener
{
Button myBut;
Communicate myCommunicateObj;
public MyObject()
{
}
public MyObject(Component parentComponent, Communicate tempCommunicate)
{
myCommunicateObj = tempCommunicate;
myBut = new Button("Click");
add(myBut);
myBut.addActionListener(this);
JDialog newObjectDialog = new JDialog((parentComponent != null) ? (Frame) parentComponent : null, "Add Object", true);
newObjectDialog.setContentPane(this);
newObjectDialog.setSize(300, 230);
myCommunicateObj.setParentWait();
newObjectDialog.show();
}
public void actionPerformed(ActionEvent ae) {
myCommunicateObj.releaseLock();
}
}
 
actually it turns out i don't need to block the thread at all- swing does it for me.<BR><BR>Once I call Dialog.show(), i don't have to wait at all. no synchronized blocks needed.<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newObjectDialog.show();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// trim all this stuff out<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return newObjectPanel.obj;<BR><BR>thanks, though. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top