Dynamically display messages in JTextArea

Hello everyone! I have a problem with my code so i hope someone can help me out. I am not very advanced to Java but i have to create a chat program. My problem is that i dont know how to display messages from different users into my JTextArea. I haven`t connected my program to a server yet because i am still working on the interface.

So how can i display dynamically messages so that it looks like an interactive chat window. Here is my code.

Please bear in mind that i will use only a username not a password.

Thanks

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.JFrame;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.JScrollPane;

publicclass Jchatextends JAppletimplements ActionListener

{

JLabel label1 =new JLabel ("Welcome to ... CHAT");

JTextField write =new JTextField(30);

JTextArea output =new JTextArea(10,35);

JButton button1 =new JButton("Send");

JButton button2 =new JButton("Clear");

Container con = getContentPane();

Font headline =new Font("Georgia", Font.ITALIC, 18);

JScrollPane scroll =new JScrollPane(con,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

JScrollPane scrollOutput =new JScrollPane(output);

publicvoid init()

{

con.setPreferredSize(new Dimension(700,400));

label1.setFont(headline);

con.setBackground(Color.gray);

con.setLayout(new BorderLayout());

con.add(label1,BorderLayout.NORTH);

con.add(output,BorderLayout.CENTER);

con.add(write,BorderLayout.SOUTH);

con.setLayout(new FlowLayout());

con.add(button1,BorderLayout.EAST);

con.add(button2);

button1.addActionListener(this);

button2.addActionListener(this);

write.addActionListener(this);

write.requestFocus();

//cannot change text in the text area screen

output.setEditable(false);

output.setLineWrap(true);

//scrollOutput.setVerticalScrollBarPolicy(38);

setContentPane(scroll);

}

publicvoid actionPerformed (ActionEvent e)

{

Object source = e.getSource();

if (source == button2)

{

write.setText(" ");

}

elseif (source == button1 || source == write)

{

String input = write.getText();

con.add(output,BorderLayout.CENTER);

output.setText("User says: " + input +'\n');

validate();

}

}

}

and the other one

<html>

<head>

<title> </title>

</head>

<body>

<APPLET

CODE="Jchat.class"

WIDTH=500

HEIGHT=300>

</APPLET>

</body>

</html>

[4545 byte] By [geriGa] at [2007-10-2 15:26:02]
# 1

Why did you repost this message?

You where given two suggestions in your last posting. You have not incorporated either suggestion in the code reposted here.

Read the answers you've been given, understand the suggestion and if a followup question is required then respond to the original posting. People reading this posting for the first time have no idea what was suggested in the last posting and will be wasting their time by making the same suggestion.

camickra at 2007-7-13 14:41:51 > top of Java-index,Desktop,Core GUI APIs...
# 2

well i reposted my question because you advised me that it was the wrong forum at first. Also for the answers given, the border layout declaration has nothing to do with my question. And what you advised me to use TextArea.append(...); is not very specific. As i mentioned i am not very advanced programmer and i would appreciate if you can explain how exactly to apply that line of coding in order to create an interactive chat.

geriGa at 2007-7-13 14:41:51 > top of Java-index,Desktop,Core GUI APIs...
# 3

> the border layout declaration has nothing to do with my question.

Maybe not, but at least it would should that you took the time to read the suggestion.

> And what you advised me to use TextArea.append(...); is not very specific.

Well, neither was your question, so I gave the best suggestion I could. If it wasn't helpfull, then I would assume you would post a followup question to clear up the confusion.

setText(...) replaces all the text in the text area.

append(...) adds the text to the bottom of the text area.

If you didn't understand how/why to use the append(...) method why would you create a new posting? Would you not post a followup question in the original posting? Are we supposed to be mind readers and guess that you can't read the API to determine how a method should be used?

Of couse maybe I don't understand what you mean by "interactive chat", in which case reposting a question with the same wording won't help get the problem solved either.

camickra at 2007-7-13 14:41:51 > top of Java-index,Desktop,Core GUI APIs...
# 4

mr.camickr, i am sorry if i created a confusion. Ok by "interactive chat" i mean, every time a user types something into the textfield, i want it to be displayed in the textarea. What my program does right now is only overriding the input messages. When i type a line " ...... something", it displays. However when i type into the textfield " ... another line" it displays over the previous message. In this case there is always only one message showed, not multiple. So here is my problem. You know how the chat works so i want to have a very simple interface of it. I hope i am more clear now with my question.

Thank you for your time

geriGa at 2007-7-13 14:41:51 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thats what I thought and I gave you solution. I explained the difference between the two methods "setText" and "append". I don't understand what is confusing you.
camickra at 2007-7-13 14:41:51 > top of Java-index,Desktop,Core GUI APIs...
# 6
Take a look at my TextAreaReader and TextAreaOutputStream. This may be what you need http://forum.java.sun.com/thread.jspa?threadID=709187&messageID=4105977#4105977
tjacobs01a at 2007-7-13 14:41:51 > top of Java-index,Desktop,Core GUI APIs...
# 7
tjacobs01 and camickr thank u for your suggestions. I will work on them and see if i get what i wanted.
geriGa at 2007-7-13 14:41:51 > top of Java-index,Desktop,Core GUI APIs...