Search code examples
javaswingjframejtextfield

How to pass JTextField from JFrame into another JFrame


When I input First Name, Last Name and other information in the JTextField of JFrame1 and then click button next, the inputted data will display all inputted data on last JFrame.
Example: this is animated gif, the last frame is what I want, cause I still don't know how to make it:

enter image description here

How can I do this? I'm sorry if this is a newbie question but I'm learning..

I am using NetBeans GUI builder.

EDIT: I created like this idk if i'm doing it right...

public class User {
    private String username;
    private String password;
    public User() {
        username = null;
        password = null;
    }

    public User getUser() {
        User user = new User();
        username = TexUsername.getText();
        return user;
    }
}

and in my DisplayFrame is i don't know what put inside

public void setUser(User user) {
    // idk what to put here... maybe the jLabel? please help
}

NEW UPDATES ON QUESTION

the button on my StudentRegistrationForm_1.java

private void jButton_NextActionPerformed(java.awt.event.ActionEvent evt) {                                             
    try {
        User user = new User(TexUsername.getText(),Password.getPassword());
        StudentRegistrationForm_3 form = new StudentRegistrationForm_3(user);
        form.setUser(user);
        this.dispose();
    } catch(Exception e){JOptionPane.showMessageDialog(null, e);}
    /*
    new StudentRegistrationForm_2().setVisible(true);
    this.dispose();*/

} 

and the class

public class User {

    private String name;
    private char[] password;

    public User(String name, char[] password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public char[] getPassword() {
        return password;
    }
}

public User getUser() {
    User user = new User(TexUsername.getText(), Password.getPassword());
    return user;
}

while the StudentRegistrationForm_3 I added a constructor and method like this

StudentRegistrationForm_3(User user) {
    name.setText(user.getName());
}
public void setUser(User user) {
    name.setText(user.getName());
}  

idk why still gave me null... even I input the value on username and password..


Solution

  • Passing information from part of your application to another will depend on the structure of your program.

    At the basic level, I would recommend wrapping the values from the first screen into some kind of custom object. Let's user User.

    User would store the properties of accountName and password as private instance fields, which would be accessible via getters.

    Basically, you would have some kind of getter on the first screen that would generate the User object and pass it back to the caller.

    The second screen would either take a User object as a parameter to the constructor or as setter.

    Presumbably, you would then pass the User object from your editor pane to your view pane within the actionPerformed method of your JButton

    For example...

    public class NewAccountPane extends JPanel {
        /*...*/
        public User getUser() {
            User user = new User();
            /* Take the values from the fields and apply them to the User Object */
            return user;
        }
    }
    
    public class AccountDetailsPane extends JPanel {
        /*...*/
        public void setUser(User user) {
            /* Take the values from the User object
             * and apply them to the UI components
             */
        }
    }
    

    And in your actionPerformed method...

    public void actionPerformed(ActionEvent evt) {
        User user = instanceOfNewAccountPane.getUser();
        instanceOfAccountDetailPane.setUser(user);
        // Switch to instanceOfAccountDetailPane
    }
    

    Updated from updates to question

    Your user object is almost right, but I would get rid of the getUser method. The User object should have no concept of the UI not should it need to interact with it directly...

    So instead of...

    public class User {
        private String username;
        private String password;
        public User() {
            username = null;
            password = null;
        }
    
        public User getUser() {
            User user = new User();
            username = TexUsername.getText();
            return user;
        }
    }
    

    I'd be tempered to do something like...

    public class User {
        private String username;
        private char[] password;
        public User(String username, char[] password) {
            this.username = username;
            this.password = password;
        }
    
        public String getUserName() {
            return username;
        }
    
        public char[] getPassword() {
            return password;
        }
    }
    

    So when you called getUser from your NewAccountPane, you would construct the User object based on the values of the fields on the form.

    Basic working example

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Passon {
    
        public static void main(String[] args) {
            new Passon();
        }
    
        private JPanel basePane;
        private EditorPane editorPane;
        private DisplayPane displayPane;
    
        public Passon() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    basePane = new JPanel(new CardLayout());
                    basePane.add((editorPane = new EditorPane()), "Editor");
                    basePane.add((displayPane = new DisplayPane()), "Display");
                    ((CardLayout)basePane.getLayout()).show(basePane, "Editor");
                    frame.add(basePane);
    
                    JPanel buttons = new JPanel();
                    JButton next = new JButton("Next >");
                    buttons.add(next);
                    frame.add(buttons, BorderLayout.SOUTH);
    
                    next.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            CardLayout layout = (CardLayout) basePane.getLayout();
                            displayPane.setUser(editorPane.getUser());
                            layout.show(basePane, "Display");
                            ((JButton)e.getSource()).setEnabled(false);
                        }
                    });
    
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class User {
    
            private String name;
            private char[] password;
    
            public User(String name, char[] password) {
                this.name = name;
                this.password = password;
            }
    
            public String getName() {
                return name;
            }
    
            public char[] getPassword() {
                return password;
            }
    
        }
    
        public class EditorPane extends JPanel {
    
            private JTextField name;
            private JPasswordField password;
    
            public EditorPane() {
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                add(new JLabel("User: "), gbc);
                gbc.gridy++;
                add(new JLabel("Password: "), gbc);
    
                gbc.gridy = 0;
                gbc.gridx++;
    
                name = new JTextField(20);
                password = new JPasswordField(20);
    
                add(name, gbc);
                gbc.gridy++;
                add(password, gbc);        
            }
    
            public User getUser() {
                User user = new User(name.getText(), password.getPassword());
                return user;
            }
    
        }
    
        public class DisplayPane extends JPanel {
    
            private JLabel name;
    
            public DisplayPane() {
                name = new JLabel();
                setLayout(new GridBagLayout());
                add(name);
            }
    
            public void setUser(User user) {
                name.setText(user.getName());
            }            
        }
    }
    

    Update with additional

    Passing values is a fundamental principle to programming.

    In your code, you have two choices to that I can see..

    In the jButton_NextActionPerformed of your StudentRegistrationForm_1 class, you are currently doing this...

    private void jButton_NextActionPerformed(java.awt.event.ActionEvent evt) {                                             
        new StudentRegistrationForm_3().setVisible(true);
        // How is StudentRegistrationForm_3 suppose to reference the User object??
        User user = new User(TexUsername.getText(),Password.getPassword());
        this.dispose();
    }                                            
    

    But there is no way for StudentRegistrationForm_3 to access the User object you have created.

    In the jButton_NextActionPerformed of your StudentRegistrationForm_1 class, you can either pass the User object to the constructor of the instance of StudentRegistrationForm_3 that you create

    private void jButton_NextActionPerformed(java.awt.event.ActionEvent evt) {                                             
        User user = new User(TexUsername.getText(),Password.getPassword());
        new StudentRegistrationForm_3(user).setVisible(true);
        this.dispose();
    }                                            
    

    Or modify StudentRegistrationForm_3 to have a method that accepts a User object

    private void jButton_NextActionPerformed(java.awt.event.ActionEvent evt) {                                             
        User user = new User(TexUsername.getText(),Password.getPassword());
        StudentRegistrationForm_3 form = new StudentRegistrationForm_3(user);
        form.setUser(user);
        this.dispose();
    }                                            
    

    Either way, you will need to modify the StudentRegistrationForm_3 class to support this.