?
Solved

Creating Objects from a GUI

Posted on 2000-03-11
4
Medium Priority
?
192 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?
0
Comment
Question by:jeslawson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
jerch earned 150 total points
ID: 2607815
Hello jeslawson...
You can simply do this. Simply use the wait and notify method of the Object class.

The code below illustrate how to use wait and notify methods.

class GUI implements ActionListener {
      private Object data = null;
      private boolean dataAvailable = false;

      public synchronized void actionPerformed(ActionEvent e) {
            data = <instantiate the object you want to return>;
            dataAvailable = true;
            notify();
      }

      public synchronized Object getMyObject() {
            while (dataAvailable == false) {
                  try {
                        wait();
                  } catch (InterruptedException e) {
                  }
            }
            dataAvailable = false;
            return data;
      }      
}

Take note that both methods have to be synchronized.  If you remove the synchronized keyword.  The program will generate an RuntimeException.  Any clarification....

Hope this helped.

Jerson
0
 

Author Comment

by:jeslawson
ID: 2743912
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?  

0
 
LVL 4

Expert Comment

by:jerch
ID: 2745894
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
0
 

Author Comment

by:jeslawson
ID: 2746838
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
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question