Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Design an application with swing and threads... but how?

Status
Not open for further replies.

frag

Programmer
Dec 7, 2000
321
GB
Hi!


I have class 'ClientWindow' which creates the GUI for the clientside of my application.
There is a JTabbedPane in it with serveral tabs. On every tab there is a button, whenever
one of these buttons is pressed the client will send an object to the server (using socket
and streams) and waits for a reply (the return of the object). I will have to get the object's
status and write it in a JTextArea on the proper tab. This should be solved by using threads.
I want a thread for every object that is send by pressing the button. This thread should
wait untill it gets the object back from the server.

As far as I understood the thread-philosphy I have to write a class that extends Thread and
overwrites the 'run()' methode. This class will be instantiated by the 'ClientWindow' class
and the run() method will be called. But how can 'clientWindow' access the processed object
that returns from the server? run() is supposed to return 'void'....

Let's pretend this is my code:

Code:
public class ClientWindow extends JFrame implements ActionListener
{

  public ClientWindow()
  {
   addWindowListener(new WindowAdapter() { public void 
     windowClosing(WindowEvent e) { actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "exit")); } } );
  
   
   // do all the GUI stuf

  }
  


  public void actionPerformed(ActionEvent e)
  {
    if("execute".equals(e.getActionCommand()))
    {
      ScriptObj so = new ScriptObj();
      ScriptThread st = new ScriptThread(so);
      st.run();
    }
    .
    .
    .

}

How can I access the object, after it was processed by calling 'run()'?
Did I design the whole thing wrong??
Every thread-example I was looking at in the internet was not dealing with
data exchange between father and child. :-(

Any help is appreciated...

Cheers

frag

patrick.metz@epost.de
 
Hello,

i am not sure but if i get it right but your problem ist how to access the data in StringObj in the ClientWindow?!

Well, very easy. You already have the handle 'so' in the ClientWindow. You can access all data in the StringObj by gettint it with so.getField() or so.fieldname or ....

The point is you create an instance of the StringObj class and give it another Thread but you can still access the class from other Threads. After all you still have the reference 'so'.

But you should be carefull when accessing the data becourse you have multiple Threads. So you need to write synchronized methods to manipulate the data or synchronize on the data you want to change by using something like:

synchronize (object) {
// do something with object
}

cheers

Malf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top