473,387 Members | 1,420 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,387 software developers and data experts.

SWING and NETWORKING related question

1
Hi,

I'm trying to construct a simple chat program (for learning purposes). I'm using the NetBeans IDE to construct a GUI with Swing components. I've run into a problem with printing text to a JTextPane:

Essentially, I have an actionPerformed method that contains instructions to print text to the pane, using "replaceSelection" . The program compiles and runs fine, but the text appears late. That is, other events happen first before the text appears.

Details: The problem occurs in a program for a chat Client communicating with a Chat server program. The server program is just a command line thing and works fine. The connection uses socket objects. I took all that code from somewhere else and it works fine.
My chat client has a JFrame that contains a JTextField for input, a "send" button, and a JTextPane for displaying the conversation.
When the client has entered a message, the corresponding actionPerformed method does three things:
1. It prints the message to the JTextPane
2. It transmits the message to the server program on the other machine
3. It disables the "Send" button and calls a "WaitForMessage" routine
The WaitForMessage routine contains an infinite loop ("while(true)") which terminates when a message is received from the server, prints the message to the textPane, and enables the "send" button again.

When the client types a message and clicks "send," the program appears to freeze. No text appears in the textPane until after the server has sent a message back. Once this happens, both the clients message and the server's message appear simultaneously in the textPane.

Does anyone understand why this delay occurs?

Thanks
Jun 14 '07 #1
1 1234
JosAH
11,448 Expert 8TB
Hi,

I'm trying to construct a simple chat program (for learning purposes). I'm using the NetBeans IDE to construct a GUI with Swing components. I've run into a problem with printing text to a JTextPane:

Essentially, I have an actionPerformed method that contains instructions to print text to the pane, using "replaceSelection" . The program compiles and runs fine, but the text appears late. That is, other events happen first before the text appears.

Details: The problem occurs in a program for a chat Client communicating with a Chat server program. The server program is just a command line thing and works fine. The connection uses socket objects. I took all that code from somewhere else and it works fine.
My chat client has a JFrame that contains a JTextField for input, a "send" button, and a JTextPane for displaying the conversation.
When the client has entered a message, the corresponding actionPerformed method does three things:
1. It prints the message to the JTextPane
2. It transmits the message to the server program on the other machine
3. It disables the "Send" button and calls a "WaitForMessage" routine
The WaitForMessage routine contains an infinite loop ("while(true)") which terminates when a message is received from the server, prints the message to the textPane, and enables the "send" button again.

When the client types a message and clicks "send," the program appears to freeze. No text appears in the textPane until after the server has sent a message back. Once this happens, both the clients message and the server's message appear simultaneously in the textPane.

Does anyone understand why this delay occurs?

Thanks
You have a thread problem: Swing (and AWT) uses a single thread for all its
display tasks, event dispatching tasks and then some.

When the user presses the 'send' button your ActionListener is invoked in that
single AWT thread. Your ActionListener does quite a bit:

1) it prints that string to the JTextPane; it arrives there but your method hogs
up the AWT thread so it can't be displayed yet.

2) it transmits the string to your server so the AWT thread still can't do what
it is supposed to do, i.e. update the display of your JTextPane.

3) it disables a button (which can't be shown because the thread is still busy)
and then the poor thread has to wait for an answer from your server. When the
answer arrives you update your JTextPane again (which works) and finally
you relinguish control so everything can be visually updated.

Perform all those actions in another thread and make your ActionListener
return immediately (after it has started that other thread). That way you don't
hog the AWT thread so it can update the display when needed immediately.

kind regards,

Jos
Jun 14 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Joseph | last post by:
hi 1) i plan on having an awt canvas component (to draw graphs) on a JFrame with other swing components..will this be okay? i've read that swing and awt aren't compatible.. 2)Also, if i...
3
by: Sherrod Faulks | last post by:
I'm using Jython and in the python script I do: from javax import swing result = javax.swing.JOptionPane.showInputDialog(wC, cmd,"Prompt from " + client.serverName, JOptionPane.PLAIN_MESSAGE) ...
1
by: Nandan | last post by:
hello, can I ask a jython question here? when I use the jython interpreter I have no problem with the statement: from java import lang from javax import swing but when I put this in a...
46
by: H.A. Sujith | last post by:
Why doesn't the standard library provide (at least basic) networking facilities using TCP/IP ?
7
by: CJ Taylor | last post by:
Hey all, Was wondering if anyone could answer a question using the Windows Scripting Host to use the Win32API to connect to a network share on an NT server. The problem I am having is that I...
2
by: asj | last post by:
I have to honestly say I was flabbergasted by this report. Obviously, I have no problems asserting that Java is #1 in the enterprise (JEE), or in the mobile space (JME), and I do know it's made...
1
by: Wayne Rasmussen | last post by:
A question was asked by someone at work who is going through the swing tutorials and updating some of his code from years ago. His code uses AWT and the 1.1 event models. If he converts to swing...
3
by: =?Utf-8?B?cHJvc3Nyb24=?= | last post by:
I have had a home network with xp for many years. Itried to add a vista laptop to this network, Ok the vista laptop cansee and access files and the printer (which is connected to the desktp) on the...
6
by: r035198x | last post by:
I have put together this article to give people starting Swing an awareness of issues that need to be considered when creating a Swing application. Most of the Swing tutorials that I have seen just...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.