Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Getting a thread to return data to the GUI periodically

Thread view: 
Pkid - 12 Feb 2007 14:39 GMT
Hi

I am new to Java and threading.  I am trying to write a GUI program
that does some networking using the NIO.  I get the GUI to create a
new instance of my object that does the networking and then call the
start() method of that object to get the thread running.  I want the
thread to be able to return data to the GUI so that I can display what
is happening in the thread on the GUI.  So if a new connection is made
I want to display that in the a textarea in the GUI.

The code sort of looks like this:

In the constructor of the GUI I do the following:

MyNetworkClass newInstance = new MyNetworkClass
newInstance.start()

The network class would do something like:

public class MyNetworkClass extends Thread
{
  public void run()
  {
      infinite loop handling networking events
  }
}
Patricia Shanahan - 12 Feb 2007 15:33 GMT
> Hi
>
[quoted text clipped - 22 lines]
>    }
> }

How about something like a listener? The network code defines an
interface with a method for each type of event it can report, with
suitable parameters to convey the details. The GUI code creates an
object that implements that interface and passes it to the network code
constructor.

In the case of a GUI, some methods in the listener object may need to
call invokeLater to get things run in the event handling thread. That is
an issue for the GUI, and not something the network code should need to
care about.

Patricia
Pkid - 13 Feb 2007 18:44 GMT
> > Hi
>
[quoted text clipped - 35 lines]
>
> Patricia

Hi

Sorry to bug you again but if I change it to the following it seems to
use less memory.  Not creating the new thread?  Thanks!

public class ServerGUIListener implements NetworkListenerInterface,
Runnable
{
    String eventDescription;
    ChatServerGUI sourceObject;

    public ServerGUIListener(ChatServerGUI new_sourceObject)
    {
        sourceObject = new_sourceObject;
    }

    public void TalkBack()
    {
        sourceObject.RaiseEvent(eventDescription);
    }

    public void run()
    {
        TalkBack();
    }

    public void EventHappened(String new_description)
    {
        eventDescription = new_description;
        javax.swing.SwingUtilities.invokeLater(this);
    }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.