1.11M Members

Help in getting input from textfield of another class into another class.

 
0
 

I have a problem with getting an input from class A to class B. I already created a method which gets the value but when I call it in class b it returns empty.
These are my codes:
CLASS A

public class log extends javax.swing.JFrame {

    public String name;
  

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
             System.out.println("1"+getuser());
        try {

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/devweb", "root", "123456");
            PreparedStatement statement = con.prepareStatement("select User,pwd from account");
            ResultSet rs = statement.executeQuery();
             name = user.getText();
            String pwd = jTextField2.getText();
      
            while (rs.next()) {
                if (rs.getString("User").equals(name) && rs.getString("pwd").equals(pwd)) {
                    user.getText();
                    result now = new result();
                    getuser();
                    now.res();
                    now.jTextField1.setText(name);
                     
                     System.out.println("2"+getuser());
                     

                }


            }




        } catch (Exception e) {
            System.out.println(e.getMessage());
        }




    }                                        
 public   String getuser() {
       
      
        return this.user.getText();
        
    }

CLASS B(I'm gonna post the button straight away since that this is the only thing that this class contains)

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       
        log qwe=new log();
        qwe.getuser();
       
        jTextField1.setText(qwe.user.getText());
        System.out.println("3"+qwe.getuser());
      
        System.out.println("5"+qwe.user.getText());
 
0
 

Line 3 you create new log, then ask that to getUser, so of course its empty.
Instead of a new log class, B needs a reference to the real log instance that you created and interacted with. How that happens depends on the code where log and B are created, which I don't see here.

 
0
 

the user variable needs to be an instantiated variable under the class, and either needs to be given a value there or in a default constructor. Infact it shouldn't even compile because I don't see where user is ever instantiated (Including jTextField2 as well).

I'll assume all the variables are textfields.

public class log extends javax.swing.JFrame {

    public JTextField user = new JTextField("", 10);
    public JTextField jTextField2 = new JTextField("", 10);
    public String name;
  
    /* Default constructor */
    public log()
    {
       user.setText("Some stuff!");
       jTextField2.setText("Some other stuff!");
    }

Now at least the getuser method will return something...
In this case, it will return "Some stuff!"

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: