473,408 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Connecting socket thread to gui

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
4 6131
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Google Mike | last post by:
After a lot of thought and research, and playing with FreeTDS and InlineTDS, as well as various ODBC connections, I have determined that the fastest and cheapest way to get up and going with PHP on...
0
by: christian_stengel | last post by:
Hi *, I have just started to learn python and I am having a problem with an python client connecting to a perl server using ssl (I tried this with pyOpenSSL and with the build in SSL Module). ...
2
by: Bruce Vander Werf | last post by:
I am developing a network client application (using the Socket class) that will need to make simultaneous TCP connections to many (100 or more) servers. In this case, which would be a better...
4
by: zbcong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a...
2
by: zhebincong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting...
3
by: O.B. | last post by:
When sharing a Socket between threads, are the socket operations automatically synchronized to support multithreading? For example: Thread1 sets up a server socket to listen and invokes...
0
by: =?Utf-8?B?QWxwZXIgQUtDQVlPWg==?= | last post by:
Hello, First of all I wish you a good day. My help request is about .NET asynchrounus socket communication. I have developed Server-Client Windows Forms .NET applications in VC++ .NET v2003. I...
5
by: Bruce | last post by:
Hello I am building a C# app that creates anywhere from 10 to 100 connections to a specified server and sends 1000s of TCP requests and processes the responses. (it is a stress tool) I planned...
1
by: ElvisRS | last post by:
Hi, I'm writing a simple udp client using sockets. My code looks like this: initServer2 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.