Reputation: 1
I have two classes:
public class VentanaPrincipal extends javax.swing.JFrame
{
MetodosFicheros objMetodosFicheros;
public void ListadoTextArea(String textLine)
{
listadoTextArea.append(textLine);
}
private void datosButtonActionPerformed(java.awt.event.ActionEvent evt)
{
objMetodos = new MetodosFicheros();
objMetodos.leerFichero();
}
}
public class MetodosFicheros
{
private VentanaPrincipal objVentana;
public void leerFichero()
{
String textLine;
objVentana.ListadoTextArea(textLine);
}
}
I would like to print "textLine" in "listadoTextArea" but it doesn't display nothing. If I use System.out.println(textLine)
instead of listadoTextArea.append(textLine)
, the console displays "textLine" correctly. So, I don't know where is the error.
I'm sorry, the most part of the code is here.
public class VentanaPrincipal extends javax.swing.JFrame
{
private String clave;
private String nombre;
private int edad;
private float sueldo;
private MetodosFicheros objMetodos;
public VentanaPrincipal()
{
initComponents();
}
public void ListadoTextArea(String textLine)
{
listadoTextArea.append(textLine);
}
private void datosButtonActionPerformed(java.awt.event.ActionEvent evt {
objMetodos = new MetodosFicheros();
objMetodos.leerFichero();
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
claveText = new javax.swing.JTextField();
edadText = new javax.swing.JTextField();
sueldoText = new javax.swing.JTextField();
nombreText = new javax.swing.JTextField();
grabarButton = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
datosButton = new javax.swing.JButton();
indicesButton = new javax.swing.JButton();
indicesDatosButton = new javax.swing.JButton();
ordenarButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
listadoTextArea = new javax.swing.JTextArea();
jPanel3 = new javax.swing.JPanel();
jLabel6 = new javax.swing.JLabel();
claveBuscarText = new javax.swing.JTextField();
indiceSinOrdenarButton = new javax.swing.JButton();
indiceOrdenadoButton = new javax.swing.JButton();
busquedaDicotomica = new javax.swing.JButton();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
nombreBuscarText = new javax.swing.JTextField();
edadBuscarText = new javax.swing.JTextField();
sueldoBuscarText = new javax.swing.JTextField();
registrosText = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
indiceText = new javax.swing.JTextField();
errorText = new javax.swing.JTextField();
}
// Variables declaration - do not modify
private javax.swing.JButton busquedaDicotomica;
private javax.swing.JTextField claveBuscarText;
private javax.swing.JTextField claveText;
private javax.swing.JButton datosButton;
private javax.swing.JTextField edadBuscarText;
private javax.swing.JTextField edadText;
private javax.swing.JTextField errorText;
private javax.swing.JButton grabarButton;
private javax.swing.JButton indiceOrdenadoButton;
private javax.swing.JButton indiceSinOrdenarButton;
private javax.swing.JTextField indiceText;
private javax.swing.JButton indicesButton;
private javax.swing.JButton indicesDatosButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea listadoTextArea;
private javax.swing.JTextField nombreBuscarText;
private javax.swing.JTextField nombreText;
private javax.swing.JButton ordenarButton;
private javax.swing.JTextField registrosText;
private javax.swing.JTextField sueldoBuscarText;
private javax.swing.JTextField sueldoText;
}
public class MetodosFicheros
{
public MetodosFicheros(){}
private VentanaPrincipal objVentana;
private String dialogo = "No se ha encontrado el fichero";
private String fileName = "FicherosDatos.dat";
private JTextField JTextField;
private JTextArea JTextArea;
/****************************************************************************************
**Metodo que graba en un fichero ".dat" los valores introducidos en la interfaz grafica**
*****************************************************************************************/
public void grabarFichero(String clave, String nombre, int edad, float sueldo)
{
objVentana = new VentanaPrincipal();
DataOutputStream fichero = new DataOutputStream(new FileOutputStream(fileName, true));
fichero.writeUTF(clave);
fichero.writeUTF(nombre);
fichero.writeInt(edad);
fichero.writeFloat(sueldo);
}
public void leerFichero()
{
String textLine;
JTextArea = new JTextArea();
FileReader fichero = null;
BufferedReader reader;
objVentana = new VentanaPrincipal();
fichero = new FileReader(fileName);
reader = new BufferedReader(fichero);
while((textLine = reader.readLine()) != null)
{
objVentana.ListadoTextArea(textLine);
}
}
}
Upvotes: 0
Views: 118
Reputation: 285430
A guess, perhaps your problem is due to changing the state of the wrong reference -- that you may have create a second object, and are changing the state of the 2nd object, not the currently displayed one.
Key here will be the objVentana variable below:
public class MetodosFicheros
{
private VentanaPrincipal objVentana;
public void leerFichero()
{
String textLine;
objVentana.ListadoTextArea(textLine);
}
}
How do you assign it a reference? Are you 100% sure that it is in fact referring to the displayed VentanaPrincipal, or is there somewhere in the code not shown a call to new VentanaPrincipal()
that creates a new reference, and that you're calling methods on that. I'll bet that this is exactly what you're doing.
But more importantly, regardless of whether I'm right or wrong, you should strive to improve your question as it is missing key information that would allow us to help you without having to guess.
Edit
Yep, I'm right. As I mentioned, all you have to do is search your code for new VentanaPrincipal()
, and any time you see that, you know that you're creating a new VentanaPrincipal window object, one that is completely distinct from the one that's being displayed:
public void leerFichero()
{
String textLine;
JTextArea = new JTextArea();
FileReader fichero = null;
BufferedReader reader;
objVentana = new VentanaPrincipal(); // ********* here **********
fichero = new FileReader(fileName);
reader = new BufferedReader(fichero);
while((textLine = reader.readLine()) != null)
{
objVentana.ListadoTextArea(textLine);
}
}
}
The solution is not to create a new VentanaPrincipal, but instead to pass into your MetodosFicheros object a reference to the currently viewed VentanaPrincipal object, and then call methods on it.
So change MetodosFicheros to
public class MetodosFicheros {
private VentanaPrincipal objVentana;
public MetodosFicheros(VentanaPrincipalobjVentana) {
this.objVentana = objVentana;
}
pass in the appropriate VentanaPrincipal when calling the above constructor,
private MetodosFicheros objMetodos = new MetodosFicheros(this);
and don't create any new VentanaPrincipal objects.
Upvotes: 1