I’m just looking for a little guidance as to how i might implement a chat box.
There needs to be a text area for user entry, and a scrolling list of past messages. Is there an easy/standard way of going about doing this?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’m not sure about the suggestion for using a JList for previous messages .. that doesn’t seem like the behavior I’d be looking for in a chat client.
I would recommend taking a look at the JTextPane class, which is intended for more “document” type data such as a running chat conversation. It is very easy to append data on the fly to the document and you can even apply styling to portions of the document (say for instance you want to show the user names of the various chat participants in bold and in different colors).
Here are a couple of links to examples:
How to Use Editor Panes and Text Panes
More TextPane specific examples
The other thing you’ll probably want to think about is threading, for instance you would likely want to have a background thread that is polling for new messages so that the UI doesn’t hang while you’re looking for new messages to display.