jsuttor
asked on
Autoscroll! HELP!
Ok I am trying to get the JFrame textarea to autoscroll to the last record written and always scroll to the last record written everytime a new record is written... can someone help me. I am new to JAVA and I am trying to learn but this seems to be beyond me.
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
public class SockPuppet5000
{
JTextArea TextArea;
ServerSocket server;
Socket client;
BufferedReader in;
PrintWriter out;
String line;
BufferedWriter fileout;
public SockPuppet5000()
{
server = null;
client = null;
in = null;
out = null;
JFrame.setDefaultLookAndFe elDecorate d(true);
JFrame frame = new JFrame("Sock Puppet 5000");
frame.setDefaultCloseOpera tion(JFram e.EXIT_ON_ CLOSE);
TextArea = new JTextArea(time()+" - Application started\n", 20,50);
TextArea.setEditable(false );
JScrollPane ScrollPane = new JScrollPane(TextArea);
frame.getContentPane().add (ScrollPan e, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public void listenSocket()
{
///*
try
{
server = new ServerSocket(9600);
}
catch(IOException ioexception)
{
TextArea.append(time()+" - Could not listen on port 9600\n");
System.exit(-1);
}
TextArea.append(time()+" - Waiting for socket\n");
try
{
client = server.accept();
}
catch(IOException ioexception1)
{
TextArea.append(time()+" - Accept failed: 9600\n");
System.exit(-1);
}
try
{
in = new BufferedReader(new InputStreamReader(client.g etInputStr eam()));
}
catch(IOException ioexception2)
{
TextArea.append(time()+" - Accept failed: 9600\n");
System.exit(-1);
}
//*/
try
{
//BufferedReader in = new BufferedReader(new FileReader("input.txt"));
int c, index=0, end=0;
int[] chr = new int[10000000];
char[] filename = new char[256], chrc=new char[1000];
String file="";
boolean data = false;
while (true)
{
c = in.read();
//TextArea.append(String.v alueOf(c)+ "\n");
if (c==1)
{
data = true;
c = in.read();
index=0;
TextArea.append(time()+" - Reading Stream\n");
}
if (c==4)
{
data = false;
end=index;
//str = "";
TextArea.append(time()+" - Searching for filename, array size:"+end+"\n");
String str = new String(chrc);
/*for (index=0; end>index; index++)
{
str=str+String.valueOf((ch ar)chr[ind ex]);
//TextArea.append("scannin g index:"+index+"\n"); //too slow
}*/
//TextArea.append("convert ed to string\n");
//TextArea.append("beginni ng of key:"+str.indexOf("<KEYWOR D>")+9+"\n ");
//TextArea.append("end of key:"+str.indexOf("</KEYWO RD>")+"\n" );
int beginkey = str.indexOf("<KEYWORD>")+9 ;
int endkey = str.indexOf("</KEYWORD>");
TextArea.append(beginkey+" -"+endkey) ;
filename[0]='n';
filename[1]='o';
if (endkey>0) str.getChars(beginkey,endk ey, filename,0);
file="";
for (index=0; endkey-beginkey>index; index++)
{
file=file+filename[index];
}
file="./xmlfiles/"+file+". xml";
TextArea.append(time()+" - Got filename:"+file+"\n");
try
{
fileout = new BufferedWriter(new FileWriter(file));
}
catch (IOException e)
{
}
TextArea.append(time()+" - Writing "+end+" bytes to: "+file+"\n");
for (index=0; end>index; index++)
{
//TextArea.append(String.v alueOf((ch ar)chr[ind ex]));
fileout.write(chr[index]);
}
fileout.close();
//TextArea.append("\n");
}
if (data)
{
if (index<999) chrc[index]= (char)c;
chr[index++] = c;
//TextArea.append("writing array\n");
}
//fileout.write(chr);
}
//in.close();
//TextArea.append(time()+" - Stream ended\n");
}
catch (IOException e)
{
}
}
protected void finalize()
{
try
{
in.close();
out.close();
server.close();
fileout.close();
}
catch(IOException ioexception)
{
System.out.println(time()+ " - Could not close.");
System.exit(-1);
}
}
public String time()
{
DateFormat fDateFormat = DateFormat.getTimeInstance (DateFormat.DEFAULT);
Date now = new Date ();
return fDateFormat.format (now);
}
public static void main(String args[])
{
SockPuppet5000 puppet = new SockPuppet5000();
puppet.listenSocket();
}
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
public class SockPuppet5000
{
JTextArea TextArea;
ServerSocket server;
Socket client;
BufferedReader in;
PrintWriter out;
String line;
BufferedWriter fileout;
public SockPuppet5000()
{
server = null;
client = null;
in = null;
out = null;
JFrame.setDefaultLookAndFe
JFrame frame = new JFrame("Sock Puppet 5000");
frame.setDefaultCloseOpera
TextArea = new JTextArea(time()+" - Application started\n", 20,50);
TextArea.setEditable(false
JScrollPane ScrollPane = new JScrollPane(TextArea);
frame.getContentPane().add
frame.pack();
frame.setVisible(true);
}
public void listenSocket()
{
///*
try
{
server = new ServerSocket(9600);
}
catch(IOException ioexception)
{
TextArea.append(time()+" - Could not listen on port 9600\n");
System.exit(-1);
}
TextArea.append(time()+" - Waiting for socket\n");
try
{
client = server.accept();
}
catch(IOException ioexception1)
{
TextArea.append(time()+" - Accept failed: 9600\n");
System.exit(-1);
}
try
{
in = new BufferedReader(new InputStreamReader(client.g
}
catch(IOException ioexception2)
{
TextArea.append(time()+" - Accept failed: 9600\n");
System.exit(-1);
}
//*/
try
{
//BufferedReader in = new BufferedReader(new FileReader("input.txt"));
int c, index=0, end=0;
int[] chr = new int[10000000];
char[] filename = new char[256], chrc=new char[1000];
String file="";
boolean data = false;
while (true)
{
c = in.read();
//TextArea.append(String.v
if (c==1)
{
data = true;
c = in.read();
index=0;
TextArea.append(time()+" - Reading Stream\n");
}
if (c==4)
{
data = false;
end=index;
//str = "";
TextArea.append(time()+" - Searching for filename, array size:"+end+"\n");
String str = new String(chrc);
/*for (index=0; end>index; index++)
{
str=str+String.valueOf((ch
//TextArea.append("scannin
}*/
//TextArea.append("convert
//TextArea.append("beginni
//TextArea.append("end of key:"+str.indexOf("</KEYWO
int beginkey = str.indexOf("<KEYWORD>")+9
int endkey = str.indexOf("</KEYWORD>");
TextArea.append(beginkey+"
filename[0]='n';
filename[1]='o';
if (endkey>0) str.getChars(beginkey,endk
file="";
for (index=0; endkey-beginkey>index; index++)
{
file=file+filename[index];
}
file="./xmlfiles/"+file+".
TextArea.append(time()+" - Got filename:"+file+"\n");
try
{
fileout = new BufferedWriter(new FileWriter(file));
}
catch (IOException e)
{
}
TextArea.append(time()+" - Writing "+end+" bytes to: "+file+"\n");
for (index=0; end>index; index++)
{
//TextArea.append(String.v
fileout.write(chr[index]);
}
fileout.close();
//TextArea.append("\n");
}
if (data)
{
if (index<999) chrc[index]= (char)c;
chr[index++] = c;
//TextArea.append("writing
}
//fileout.write(chr);
}
//in.close();
//TextArea.append(time()+"
}
catch (IOException e)
{
}
}
protected void finalize()
{
try
{
in.close();
out.close();
server.close();
fileout.close();
}
catch(IOException ioexception)
{
System.out.println(time()+
System.exit(-1);
}
}
public String time()
{
DateFormat fDateFormat = DateFormat.getTimeInstance
Date now = new Date ();
return fDateFormat.format (now);
}
public static void main(String args[])
{
SockPuppet5000 puppet = new SockPuppet5000();
puppet.listenSocket();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER