Discussion:
Problemas actualizar datos JTextArea en tiempo real
(demasiado antiguo para responder)
Carlos
2004-12-27 18:12:57 UTC
Permalink
Mi problema es el siguiente:

Tengo dos clases (LeerFicheroTexto y MiFrame), en la primera lo unico
que hago es mediante un metodo recuperar los datos de un fichero de texto(
mas o menos unas 1500 lineas) y en la segunda tengo un jframe con un jbutton
y un jtextarea. Cuando pulso el boton leo el fichero (linea a linea) y le
paso los
datos al jtextarea, mi duda surge cuando veo que el jtextarea no se
actualiza en "tiempo real" sino que lo hace cuando el metodo de la primera
clase a terminado.

¿Esto se puede hacer no?

Echadme un cable porque ando un poco perdido. Es evidente que algo estoy
haciendo mal, pero lo que no se es que XDDDD.

Aquí os pongo el código de las clases:


LeerFicheroTexto.java

package Ejemplo;
import java.io.*;
import javax.swing.JTextArea;
public class LeerFicheroTexto
{
public LeerFicheroTexto()
{
}
public void leerFichero(JTextArea areaLog)
{
try
{
BufferedReader br=new BufferedReader(new FileReader("c:/log.txt"));
String linea="";
while ((linea=br.readLine())!=null)
{
areaLog.append(linea);
System.out.println(linea);//Lo utilizo para ver si el jtextarea se
actualiza cuando debe ;)
areaLog.append("\n");
}
br.close();
}
catch(IOException i)
{
System.out.println(i.toString());
}
}
}


MiFrame.java
package Ejemplo;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class MiFrame extends JFrame implements ActionListener
{
JButton boton1;
JTextArea areaLog;
public MiFrame()
{
super("Mi frame");
setSize(500,400);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent w)
{
dispose();
System.exit(0);
}
});
boton1=new JButton("Leer fichero");
boton1.addActionListener(this);
getContentPane().add(boton1,BorderLayout.NORTH);
areaLog=new JTextArea("",30,30);
areaLog.setBackground(Color.white);
getContentPane().add(areaLog,BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed (ActionEvent a)
{
if (a.getSource()==boton1)
{
areaLog.setText("");
new LeerFicheroTexto().leerFichero(areaLog);
}
}
public static void main(String args[])
{
new MiFrame();
}
}
Carlos
2004-12-28 18:30:56 UTC
Permalink
Tema solucionado, esta es la solución que me han comentado, la dejo aquí por
si alguien le quiere echar un vistazo. Faltan algunas cosillas como añadirle
un JScrollPane al JTextarea XDDDD.



LeerFicheroTexto.java

package Ejemplo;
import java.io.*;
import javax.swing.JTextArea;
public class LeerFicheroTexto implements Runnable
{
private JTextArea areaLog;
public LeerFicheroTexto(JTextArea areaLog)
{
this.areaLog=areaLog;
}
public void leerFichero()
{
try
{
BufferedReader br=new BufferedReader(new FileReader("c:/log.txt"));
String linea="";
while ((linea=br.readLine())!=null)
{
areaLog.append(linea);
System.out.println(linea);//Lo utilizo para ver si el jtextarea se
//actualiza cuando debe ;)
areaLog.append("\n");
}
br.close();
}
catch(IOException i)
{
System.out.println(i.toString());
}
}
public void run()
{
leerFichero();
}
}




MiFrame.java
package Ejemplo;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class MiFrame extends JFrame implements ActionListener
{
JButton boton1;
JTextArea areaLog;
public MiFrame()
{
super("Mi frame");
setSize(500,400);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent w)
{
dispose();
System.exit(0);
}
});
boton1=new JButton("Leer fichero");
boton1.addActionListener(this);
boton1.setSize(500,30);
getContentPane().add(boton1);
areaLog=new JTextArea("",500,370);
areaLog.setBackground(Color.white);
getContentPane().add(areaLog);
setVisible(true);
}
public void actionPerformed (ActionEvent a)
{
if (a.getSource()==boton1)
{
areaLog.setText("");
Thread hilo=new Thread(new LeerFicheroTexto(areaLog));
hilo.start();
}
}
public static void main(String args[])
{
new MiFrame();
}
}

Loading...