import java.io.*; import javax.swing.*; import java.awt.event.*; import java.awt.BorderLayout; /** * Aplicación de ejemplo para ver un archivo de texto. *
* Uso: fileName JBetterFileViewer de Java
*/
public class JBetterFileViewer
extends javax.swing.JFrame
{
static private JBetterFileViewer theViewer;
/**
* Número predeterminado de filas del visor
*/
static public final int DEFAULT_ROWS = 40;
/**
* Número predeterminado de columnas del visor
*/
static public final int DEFAULT_COLS = 80;
private JTextArea text;
/**
* Visor construido con nombre.
*
* @param path String nombre del visor.
*/
public JBetterFileViewer( String path )
{
super( path );
setDefaultCloseOperation( EXIT_ON_CLOSE );
text = new JTextArea( DEFAULT_ROWS, DEFAULT_COLS );
text.setLineWrap( true );
getContentPane().add(
new JScrollPane( text,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
),
BorderLayout.CENTER
);
pack();
}
/**
* Añade texto al visor.
*
* @param s Texto que se va a añadir.
*/
public void append( String s )
{
text.append( s );
}
/**
* Carga el contenido del archivo de texto en el visor
*
* en el nombre de ruta de los parámetros del archivo
* @exception java.io.IOException
*/
public void load( String path )
throws IOException
{
FileReader in = new FileReader( path );
int nread;
char [] buf = new char[ 512 ];
while( ( nread = in.read( buf ) ) >= 0 )
{
Update update = new Update( buf, nread );
SwingUtilities.invokeLater( update );
}
in.close();
}
private class Update
implements Runnable
{
private final String theString;
public Update( char [] b, int n )
{
theString = new String( b, 0, n );
}
public void run()
{
theViewer.append( theString );
}
}
public static void main( String[] args )
{
if ( args.length != 1 )
{
System.err.println( "uso: