When you, say , got 50 lines of messages where the text area can show 20
lines of messages, it always shows the first 20 lines of messages not the
last 20 lines of messages. I want it to show the last 20 lines everytime a
new message arrives. Is there a way to set the pane or textarea to do the
job like i have described.
following is the source code:
ChatFrame.java
****************************************************************************
******
package myprojects.chat;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.DatagramPacket;
import java.io.*;
import java.util.Date;
import javax.swing.text.*;
/**
* @(#)Chat.java
*
* JFC Sample application
*
* @author
* @version 1.00 04/08/26
*/
public class ChatFrame extends JFrame
{
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JTextArea txtOuput = new JTextArea();
JScrollPane jScrollPane1 = new JScrollPane(txtOuput);
BorderLayout borderLayout2 = new BorderLayout();
JScrollBar scroller=new JScrollBar();
JTextField txtMessage = new JTextField();
BorderLayout borderLayout3 = new BorderLayout();
JButton btnSend = new JButton();
private int w=800;
private int h=600;
InetAddress group = null;
MulticastSocket socket = null;
String strIP = "228.5.6.7";
int nPort = 6789;
JDesktopPane panel=new JDesktopPane();
/**
* The constructor.
*/
public ChatFrame()
{
jScrollPane1.setAutoscrolls(true);
jScrollPane1.setEnabled(true);
jScrollPane1.setWheelScrollingEnabled(true);
jScrollPane1.setVerticalScrollBar(scroller);
scroller.setValueIsAdjusting(true);
scroller.setAlignmentX(JScrollBar.BOTTOM_ALIGNMENT);
jScrollPane1.setViewportView(txtOuput);
txtOuput.setPreferredSize(null);
/*
*btnSend.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnSend_actionPerformed(e);
}
});
*
**/
Container c=getContentPane();
JInternalFrame f=new JInternalFrame("Walkie
Talkie",true,false,true,true);
f.getContentPane().setLayout(borderLayout1);
f.setBounds(0,0, 0, 0);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
jPanel2.setMinimumSize(new Dimension(20, 20));
jPanel2.setPreferredSize(new Dimension(20, 20));
jPanel2.setLayout(borderLayout3);
jPanel1.setLayout(borderLayout2);
txtMessage.setText("Hello");
txtOuput.setWrapStyleWord(true);
txtOuput.setLineWrap(true);
txtMessage.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
txtMessage_actionPerformed(e);
}
});
btnSend.setToolTipText("");
btnSend.setText("Send");
btnSend.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnSend_actionPerformed(e);
}
});
borderLayout2.setHgap(50);
borderLayout2.setVgap(50);
f.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(txtOuput, null);
f.getContentPane().add(jPanel2, BorderLayout.SOUTH);
jPanel2.add(txtMessage, BorderLayout.CENTER);
jPanel2.add(btnSend, BorderLayout.EAST);
try
{
group = InetAddress.getByName(strIP);
socket = new MulticastSocket(nPort);
socket.joinGroup(group);
}
catch(Exception e)
{
e.printStackTrace();
}
Thread listener = new Thread()
{
public void run()
{
while(true)
{
byte[] buf = new byte[1000];
DatagramPacket recv = new DatagramPacket(buf, buf.length);
try
{
socket.receive(recv);
}
catch (IOException ex)
{
ex.printStackTrace();
}
String strDate = (new Date()).toString();
txtOuput.append(recv.getAddress().getHostName().toString()+":"+strDate+","+n
ew String(recv.getData()).substring(0, recv.getLength())+"\n");
Toolkit.getDefaultToolkit().beep();
ChatFrame.this.show();
}
}
};
listener.start();
f.setVisible(true);
f.setSize(300,300);
panel.add(f);
c.add(panel);
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
JMenuItem menuFileNew = new JMenuItem();
menuFile.setText("File");
menuFileExit.setText("Exit");
menuFileNew.setText("new Frame");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
ChatFrame.this.windowClosed();
}
}
);
menuFileNew.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JInternalFrame f=new
JInternalFrame("frame",true,true,true,true);
f.setVisible(true);
f.setSize(400,400);
panel.add(f);
add(panel);
}
}
);
menuFile.add(menuFileExit);
menuFile.add(menuFileNew);
menuBar.add(menuFile);
setTitle("ChatFrame");
setJMenuBar(menuBar);
setSize(new Dimension(w, h));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ChatFrame.this.windowClosed();
}
}
);
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is save to close the application
// Exit application.
System.exit(0);
}
private void btnSend_actionPerformed(ActionEvent e)
{
String msg = txtMessage.getText();
DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(),
group, nPort);
try
{
socket.send(hi);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
private void txtMessage_actionPerformed(ActionEvent e)
{
btnSend_actionPerformed(e);
}
}
*************************************************
Chat.java
*************************************
package myprojects.chat;
public class Chat {
public static void main(String[] args) {
// Create application frame.
ChatFrame frame = new ChatFrame();
// Show frame.
frame.show();
}
}