Getting a thread to return data to the GUI periodically

P

Pkid

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
}
}
 
P

Patricia Shanahan

Pkid said:
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
}
}

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
 
P

Pkid

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

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);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top