Solved

Creating Objects from a GUI

Posted on 2000-03-11
4
178 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
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
jerch earned 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what is the difference between "sudo su" and "su - root" 6 102
JavaScript/Java - Changing an image background color 4 54
Java Loop 6 48
diffSum example 4 21
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now