Solved

Creating Objects from a GUI

Posted on 2000-03-11
4
187 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 40
How to convert from xls to xlsx using java 7 95
Notify sent to other threads in Java 9 43
restrict decimal places for double datatype 10 34
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

726 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