I am trying to read the content of a JTextField inside a JPanel in a JFrame from another Java Class in another file.
So in one .java file I have this code:
public class Ventana extends JFrame implements ActionListener {
public Ventana() {
setSize(500, 500);
setTitle("Ventana");
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel();
grafica();
}
public void actionPerformed(ActionEvent e) {
}
public void panel() {
JPanel panel = new JPanel();
panel.setBounds(0, 0, 500, 50);
panel.setVisible(true);
panel.setBackground(Color.DARK_GRAY);
JLabel etiqueta1 = new JLabel("A: ");
JLabel etiqueta2 = new JLabel("B: ");
JLabel etiqueta3 = new JLabel("C: ");
JTextField cuadroTexto1, cuadroTexto2, cuadroTexto3;
cuadroTexto1 = new JTextField(" ");
cuadroTexto2 = new JTextField(" ");
cuadroTexto3 = new JTextField(" ");
JButton boton = new JButton("Calcular");
panel.add(etiqueta1);
panel.add(cuadroTexto1);
panel.add(etiqueta2);
panel.add(cuadroTexto2);
panel.add(etiqueta3);
panel.add(cuadroTexto3);
panel.add(boton);
this.getContentPane().add(panel, BorderLayout.NORTH);
this.validate();
}
// more code under...
And in another .java file I am trying:
public class Controlador implements ActionListener{
private Ventana vista;
private Datos modelo;
public Controlador( Ventana vista , Datos modelo){
this.vista = vista;
this.modelo = modelo;
}
//Inicia los valores del jFrame Ventana con los datos del MODELO "Datos".
public void iniciar_vista(){
vista.setTitle( "Demo" );
}
public void actionPerformed(ActionEvent e) {
modelo.setA( Integer.valueOf( vista.cuadroTexto1.getText() ) );
}
}
So what I am trying to do is in the last line of this code to access to "cuadroTexto1", read the consent to send it to another variable but I don't know how to do it.
I am not sure if you have all info you need to provide an answer. If that's the case I will answer any question you have.
you need to declare cuadroTexto1 before the constructor
public class Ventana extends JFrame implements ActionListener {
JTextField cuadroTexto1; //declaration
public Ventana() {
setSize(500, 500);
setTitle("Ventana");
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel();
grafica();
you may also need to add a getter for cuadroTexto1 depending on where is the cuadroTexto1 java file