Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes Chapter 15 SimpleChatClient Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Chapter 15 SimpleChatClient" Watch "Chapter 15 SimpleChatClient" New topic
Author

Chapter 15 SimpleChatClient

Sarah Riezen
Greenhorn

Joined: Apr 09, 2012
Posts: 6
Herro,

I'm new to learning programming in general and I've been having an issue with the SimpleChatClient in chapter 15. It connects and seems to run fine, but the JTextArea incoming doesn't show the messages being sent. Instead, it comes up in either the command, if I'm running it from there, or in eclipse's Console tab. Also the vertical scrollbar doesn't seem to show up, despite me setting it to ALWAYS visible. Either way, here's the code (don't mind my //notes, they're just to help me organize):




I've even tried to change mainPanel.add(qScroller) to mainPanel.add(incoming) to see if that was the problem, but it didn't do anything differently.

Oh, also, the server client is the ready-bake code, but it works. Basically I just don't know how to get it to print the message in the incoming JTExtArea.

Any help would be greatly appreciated. Thank you so much

-Noa
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Sarah Jane wrote:Also the vertical scrollbar doesn't seem to show up, despite me setting it to ALWAYS visible.


I suspect this means that the scroller component isn't visible at all. (That would explain why you don't see any text when you change the JTextArea inside that scroller.)

So, you were writing networking code, and you casually decided to throw in a little GUI to test it, and now the little GUI isn't as simple as you thought it would be. If I'm not mistaken the default layout manager is a BorderLayout. It looks like you've added all of your components into the same part of the BorderLayout, which isn't the right thing to do. But let me move this post over to the Swing forum, where people who are actually good at Swing hang out.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

And welcome to the Ranch!
Sarah Riezen
Greenhorn

Joined: Apr 09, 2012
Posts: 6
Paul Clapham wrote:I suspect this means that the scroller component isn't visible at all. (That would explain why you don't see any text when you change the JTextArea inside that scroller.)

So, you were writing networking code, and you casually decided to throw in a little GUI to test it, and now the little GUI isn't as simple as you thought it would be. If I'm not mistaken the default layout manager is a BorderLayout. It looks like you've added all of your components into the same part of the BorderLayout, which isn't the right thing to do. But let me move this post over to the Swing forum, where people who are actually good at Swing hang out.


I just copied what was in the book, Head First Java, 2nd Edition. I'm sure there's more efficient ways to make this stuff, but this is my first programming language I'm learning so it'll be awhile before I worry about that.

I just don't understand why it won't update in the JTextArea ><

Thanks for moving it to the right place, and for the welcome.

EDIT:
I would assume it has something to do with the incoming.append(message + "\n") on line 80, but I'm not sure.
Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

On lines 35 -36 try adding them to the main panel with some kind of layout. I think your objects are getting stacked on top of eachother, sorry I can't test it for you the computer I'm using doesn't have a JDK. It should look something like this.

Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

I just copied what was in the book, Head First Java, 2nd Edition. I'm sure there's more efficient ways to make this stuff, but this is my first programming language I'm learning so it'll be awhile before I worry about that.


It's a good book, that's the one that I used to learn Java and they have a lot of really good examples in there. You just have to be careful because some of the examples are a little bit off sometimes.
Sarah Riezen
Greenhorn

Joined: Apr 09, 2012
Posts: 6
Charles Burton wrote:On lines 35 -36 try adding them to the main panel with some kind of layout. I think your objects are getting stacked on top of eachother, sorry I can't test it for you the computer I'm using doesn't have a JDK. It should look something like this.



It worked! It made the outgoing really big but I'll play around with it. Thank you so much though!
Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

I'm glad it worked for you, so to describe what was happening in a little more detail. Because you weren't using a layout manager all of your components were getting stacked on top of one another. Your code was actually working fine, but because the component that was displaying it was underneath another one you couldn't see it happening. I do recommend taking the time to learn GridBagLayout, it can be fairly complicated, but it really is good to know gives you much more control over how the gui looks. There is a bit of a learning curve, but once you get it, it's actually not too bad. I'v actually used it to dynamically generate filter buttons and whatnot in a nice neat grid.

http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Good luck!
Sarah Riezen
Greenhorn

Joined: Apr 09, 2012
Posts: 6
I'll take a look at it today.

It didn't look like anything was over on top of anything. It looked like the JTextArea was just a white square in the GUI, getting nothing. I'm definitely interested in learning more about different layouts, since the book only touched base on "Flow" layout and "BoxLayout", just barely. I heard Java's the best language to start with, and I'm sure there are alot of mistakes to be made, but can't learn without 'em ^_^

Thanks again
Aidan Whitehall
Greenhorn

Joined: Jan 02, 2013
Posts: 1
I've just had the same issue, and was stumped until I came across this reply http://www.daniweb.com/software-development/java/threads/405135/problem-with-chat-cliens on the DaniWeb site.

It appears to be nothing more than that the JTextArea is too large for the frame that contains it.

You can either reduce the size of the incoming JTextArea, so that:



becomes



or increase the size of the chat client's frame, so that:



becomes



and Bob's your uncle!
 
jQuery in Action, 2nd edition
 
subject: Chapter 15 SimpleChatClient
 
Similar Threads
problem with program
While loop problem
Best way for login authentication
Usability features
I need help improving this chat server and chat client!