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>

