Solved

Creating Objects from a GUI

Posted on 2000-03-11
4
175 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Increment alphanumeric sequence 6 79
return in catch statement 1 40
solarwind tftp server 2 30
bitbucket vs gitbucket 3 29
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

15 Experts available now in Live!

Get 1:1 Help Now