Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding question
#1
So I'm in the process of doing my senior project (college senior, CS major) and I'm currently stuck on this one bug that I can't figure out. The project is a simple chatting client (think AIM, only less fancy) that I'm making for our ITS department to use as an alternative means of contact for students with questions, and I'm working in Java.

Now that the background is done, here's the actual bug. When I send a message from one client to the other, they both display the messages. That's normal. When it reaches the bottom of the JTextArea I'm using to display the messages, however, problems happen. The SENDING client has no issue - the window scrolls down automatically to display the most recent line. The RECEIVING client, however, does not scroll down until either the user does it manually or that client sends a message, at which point it jumps to the bottom as it should be doing. The code I'm using to update the JTextArea is
Code:
convo.setText(convo.getText() + makeTimeStamp() + "Me: " + s);
when sending, and
Code:
convo.setText(convo.getText() + makeTimeStamp() + "Client: " + s + "\n");
when receiving. The variable s in both cases is the string that's getting displayed, the names "Me" and "Client" are there as temporary for now, and the makeTimeStamp() method just creates a timestamp and returns it as a string. The newline is appended to the received message and not to the sent one because of the way the data streams work. Before sending, I trim all whitespace and then add one newline, so sending s has "\n" on the end. When I use readLine() on my input stream, however, it reads until it finds a newline and takes everything before it, so I have to add on the newline manually.

The point I'm making is that other than the slight formatting difference with how the newline is appended, the code for each of these setText() calls is identical, thus my issue with finding the bug. The only thing that I can think of that could be causing it is that the receiving setText() call is called from a second thread, which I have as a stream listener so that the program isn't locked up at all times and I can actually send messages and do other things as well. If the fact that a setText() from a second thread and not the main thread is causing it, does anyone have a solution to this? And if not, does anyone have any idea what might be causing my problem?
Reply


Messages In This Thread
Coding question - by Lord Xela - 2011-01-23, 05:30 PM
Coding question - by Nikkey - 2011-01-23, 05:45 PM
Coding question - by Lord Xela - 2011-01-23, 06:11 PM
Coding question - by Spaz - 2011-01-23, 08:43 PM
Coding question - by Lord Xela - 2011-01-23, 09:09 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)