We help IT Professionals succeed at work.

Creating Objects from a GUI

jeslawson
jeslawson asked
on
213 Views
Last Modified: 2010-03-30
I have a routine that creates a GUI in order to get user input.  Once the user has finished inputting their details I want to create a new object that is then operated on by the routine that called the creation of the GUI like so

public void routine{
Component gui = createGUI();
pane.add(g)
// should wait for user input here?
myObject o = gui.getMyObject();
o.foo();
}

There is obviously a synchronisation problem here as you shouldn't be able to operate on the object until the user's finished inputting their data.
At the minute I have action listeners on the GUI components that work fine, so getting the details and creating the object isn't the problem, it's getting at the object AFTER it's been created - at the minute the system creates the GUI but then executes the operation on the object (which is of course null)

How can I make the system wait until the object has been created by the user's input to the GUI before operating on it?
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I have tried using the code above but for some reason the system gets stuck waiting.  As well as the above methods which are in a class (GUI) , the calling routine looks like this and is in a different class. Is there a problem with this?

{
GUI gui = new GUI();
Component j = gui.createGUI();
desktop.add(j);
myObject o = gui.createObject();
}

Here's the code I used in the GUI class

public class GUI{
Object object
boolean dataAvailable;

public synchronized Object createObject(){

Object o = new Object (x,y,z);
dataAvailable = true;
notify();
}

public synchronized Object createObject(){
while (ready==false){
try{
wait();
}
catch(InterruptedException e){}
dataAvailable = false;
return object;
}





However no GUI is created on screen when this code is executed.

Is the problem to do with threads of execution?  Must the calling routine in the main class be synchronized as well?  

Commented:
Which one is the createObject, you have two versions? Remember that if you invoke the wait method, the wait method will be blocked which means that the  statements after the wait will only be invoked after the wait method is notified. And another point, do not call the wait method or call method that calls the wait method inside a listener method, other events will not respond if you do this.

cheers...
Jerson

Author

Commented:
Sorry, the code should read:

public synchronized Object createObject(){

Object o = new Object (x,y,z);
dataAvailable = true;
notify();
}

public synchronized Object returnObject(){
while (ready==false){
try{
wait();
}
catch(InterruptedException e){}
dataAvailable = false;
return object;
}



Then another class calls

{
GUI gui = new GUI();
Component j = gui.createGUI();
desktop.add(j);
myObject o = gui.returnObject();
}

createObject is called as a routine which is inside an action listener method attatched to a Jbutton

public void actionPerformed (ActionEvent e)
{

String x = textfield.getText();
//... get other parameters here

createObject(x,y,z);
}

Is that the problem, that wait is being called by a method that is called by an actionListener?
Where and how can create object be called?  Should the GUI be closed first?  Should the calling routine call createObject?

Thanks
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.