1 Reply Latest reply: Nov 12, 2013 6:23 AM by PawanPinjarkar RSS

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

    a6f814a5-90db-4080-96fb-d277d50837c0

      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 my friends !

      The code :

       

      [code]

      package Testes;

       

      public class Class1 extends javax.swing.JFrame {

       

          public Class1() {

       

              initComponents();

       

          }

       

          // <editor-fold defaultstate="collapsed" desc="Generated Code">                         


          private void initComponents() {

       

              field = new javax.swing.JTextField();

       

              botton = new javax.swing.JButton();

       

              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

       

              getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

       

              getContentPane().add(field, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 39, 336, 38));

       

              botton.setText("Botton");

       

              botton.addActionListener(new java.awt.event.ActionListener() {

       

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

       

                      bottonActionPerformed(evt);

       

                  }

       

              });

       

              getContentPane().add(botton, 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.receiceFildValueOfClass2= returnField();

       

              System.out.println(classTwo.receiceFildValueOfClass2);

       

          }                                     

       

      String returnField(){

       

           return field.getText().toString();

       

          }

       

              public static void main(String args[]) {

             

               new Class1().setVisible(true);

                 

           }

       

       

          // Variables declaration - do not modify                    

       

      private javax.swing.JButton botton;

       

          private javax.swing.JTextField field;

       

          // End of variables declaration                  

       

      }

       

       

       

      CLASS 2 :

       

      package Testes;

       

      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){

          

              }

         

         

      }

      [/code]


      Thankyou my friends !

        • 1. Re: Write the value of a JTextField to a variable of type String from another class (JAVA)
          PawanPinjarkar

          Your code is not printing the values entered in textfield from Class1 for obvious reasons. I suggest to again look carefully in your code and understand the problem. The solution for your reference is as below -

           

          Changes in Class1 -

          private void bottonActionPerformed(java.awt.event.ActionEvent evt) {

            Class2 classTwo = new Class2(returnField());

            System.out.println(classTwo.receiveFieldValueOfClass1);

            }

           

          Changes in Class2 -

          public Class2(String input) {

           

           

            receiveFieldValueOfClass1 = input;

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

            }

           

          Although, the solution is mentioned here, I suggest you to understand the issue in your code.