Search code examples
javaswingvariablesjtextfield

Write the value of a JTextField to a variable of type String from another class (JAVA)


I am very difficult to record the value typed into a JTextField that belongs to class 1 in variable of type String that belongs to class 2, for example, if I type the word "Test" in the JTextField class 1 when i press the button in class 1, I would like the value entered in the JTextField class 1 is recorded on a variable type String in class 2, so I use this variable as I want, can you give me a hand with that? I've tried to create an object of class 1 and use the getText and to String, but no success yet. When i see the System in the class2 the result is NULL ! :(

Thank you.

The code :

public class Class1 extends javax.swing.JFrame {

    public Class1() {    
        initComponents();    
    }    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          

    private void initComponents() {
        fieldOfClass1 = new javax.swing.JTextField();    
        bottonOfClass1 = new javax.swing.JButton();    
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);    
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());    
        getContentPane().add(fieldOfClass1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 39, 336, 38));    
        bottonOfClass1.setText("Botton");    
        bottonOfClass1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {    
                bottonActionPerformed(evt);    
            }    
        });

        getContentPane().add(bottonOfClass1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 114, 161, 45));
        pack();

    }// </editor-fold>  
    private void bottonActionPerformed(java.awt.event.ActionEvent evt) {                                       

Class2 classTwo = new Class2();

    classTwo.receiveFieldValueOfClass1= returnField();
        System.out.println(classTwo.receiveFieldValueOfClass1);
    } 

    String returnField(){    
      return fieldOfClass1.getText().toString();
    }    

    public static void main(String args[]) {
         new Class1().setVisible(true);
    }

    // Variables declaration - do not modify  
    private javax.swing.JButton bottonOfClass1;
    private javax.swing.JTextField fieldOfClass1;
    // End of variables declaration
}

Class 2 :

public class Class2 {

    public String receiveFieldValueOfClass1;

    Class2(){
        System.out.println("This is a valor of Jtext Field Class 1 ! = "+receiveFieldValueOfClass1);
    } 

    public static void main (String[]args){         

    }
}

Thanks


Solution

  • IIRC, you'd like to set a variable (let's name it s) in Class2 to the value of JTextField in Class1?

    Just add a public(!) method to Class2 that takes a String as argument and sets the value of s to the given String.
    In Class1 just create an ActionListener for the button which calls the public method of Class2 with the value of JTextField as argument.

    I could help you more if you'd include a code sample in your question.