By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,349 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

Connecting socket thread to gui

P: n/a
I have a thread that reads from a socket and I want to put the data
that's read into a JTextArea in my gui. What would be the prettiest
way of doing this? Listeners? Passing the JTextArea to the thread?
Thanks in advance for any answers.

Wenslauw
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ak
> I have a thread that reads from a socket and I want to put the data
that's read into a JTextArea in my gui. What would be the prettiest
way of doing this? Listeners? Passing the JTextArea to the thread?


I suggest you to use BufferedReader#readLine() to get next String from
sockets InputStream and append it to JTextArea.

--
http://uio.dev.java.net
http://reader.imagero.com
Jul 17 '05 #2

P: n/a
"Wenslauw" <we******@xs4all.nl> wrote in message
news:87**************************@posting.google.c om...
I have a thread that reads from a socket and I want to put the data
that's read into a JTextArea in my gui. What would be the prettiest
way of doing this? Listeners? Passing the JTextArea to the thread?
Thanks in advance for any answers.

Wenslauw


Use SwingUtilities.invokeLater(Runnable) to send
a message from your socket thread to the AWT Event
Dispatch Thread (EDT). The Runnable object updates
the gui under the EDT context.
Jul 17 '05 #3

P: n/a
xarax wrote:
"Wenslauw" <we******@xs4all.nl> wrote in message
news:87**************************@posting.google.c om...
I have a thread that reads from a socket and I want to put the data
that's read into a JTextArea in my gui. What would be the prettiest
way of doing this? Listeners? Passing the JTextArea to the thread?
Thanks in advance for any answers.

Wenslauw

Use SwingUtilities.invokeLater(Runnable) to send
a message from your socket thread to the AWT Event
Dispatch Thread (EDT). The Runnable object updates
the gui under the EDT context.


JTextArea.append() is thread safe and does not require
SwingUtilities.InvokeLater() to work safely.

--

Knute Johnson
email s/nospam/knute/
Molon labe...
Jul 17 '05 #4

P: n/a
Wenslauw wrote:
I have a thread that reads from a socket and I want to put the data
that's read into a JTextArea in my gui. What would be the prettiest
way of doing this? Listeners? Passing the JTextArea to the thread?
Thanks in advance for any answers.

I created a class, StringSocketThread that takes a Socket on creation
and then wraps itself inside a Thread (it implements Runnable) and
starts listening to the socket. I think of it as a Socket Agent. When a
line is received, or the socket state changed, all listeners are
informed. I defined the listener interface internally, so listening
classes must implement StringSocketThread.Listener.

I don't believe there are any problems with this approach, but I'm not
one hundred percent certain, which is why I post this message here.
Please inform me if I'm missing something. I think I have a basic
understanding of thread safety, but you never know..

Geeetz,
Pieter Bonne.

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.