0

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..

JeraldPunx
  • 309
  • 3
  • 8
  • 18
  • 1
    First things first, you should avoid multiple frames, have a look at [The use of multiple frames, good or bad practice](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice). Instead take a look at using [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – MadProgrammer Aug 18 '13 at 06:39
  • yeah but it's hard to understand... and I'm using netbeans GUI and don't know how to use that – JeraldPunx Aug 18 '13 at 07:05
  • It's not going to get any easier – MadProgrammer Aug 18 '13 at 08:46
  • yeah i'm trying but the problem is when I press next... I want the next changed again like I did here... I don't know how to set up that... i just want three button = back, next and cancel... then when I press next the page next but i can't next anymore cause i just show that layout... – JeraldPunx Aug 18 '13 at 11:35
  • There is where you would use some kind of model to control the flow. Using multiple frames in this nature is so VB6, we can do so much better – MadProgrammer Aug 18 '13 at 11:46

1 Answers1

4

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.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • do i need to create a new file and insert like this? or I put on the first panel under inintComponents()? sorry for being noob... – JeraldPunx Aug 18 '13 at 07:07
  • hello i really don't understand... is there a tutorial on youtube or anything... or any lesson where do I learn something like that... so the sample u gave is the NewAccountPane is where I input some information and the AccountDetailsPane is where is the result? what should i code on the below of User user= new User(); and on the public void setUser(User user) sorry for being noob... I really want to learn this thing – JeraldPunx Aug 18 '13 at 11:31
  • 1
    This is really basic, principle stuff, sorry, but it is. You could take a look at [Passing Information to a Method or a Constructor](http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html), but I recommend going right back to [core basics](http://docs.oracle.com/javase/tutorial/java/concepts/index.html), Swing is an advance enough API with the basics behind you – MadProgrammer Aug 18 '13 at 11:45
  • hello I updated my question... but still not figuring it out... can I ask if i miss something? – JeraldPunx Aug 25 '13 at 03:56
  • 1+ Although I'd perhaps not using Strings to hold password information. – Hovercraft Full Of Eels Aug 25 '13 at 04:33
  • @HovercraftFullOfEels : Exactly that's what I was about to complain, upvoted the answer long time back :-) – nIcE cOw Aug 25 '13 at 04:35
  • 1
    Okay, okay...thought I made a comment about that a while back :P – MadProgrammer Aug 25 '13 at 04:38
  • 1
    Its hard to say without your code, but something like userLabel.aetText(user.getUserName()); – MadProgrammer Aug 25 '13 at 06:15
  • I add `User user = new User()` on my displayFrame because i can't create like this `name.setText(user.getUserName())` but I already type this on my firstFrame `User user = new User(TexUsername.getText(),Password.getPassword())` idk what to put on 2nd frame now... there is no getText/getPassword on my 2nd frame...note: this is not CardLayout I just make this on by jFrame cause i don't know how to create nextpage thing.. – JeraldPunx Aug 25 '13 at 07:01
  • I only see like this when I add `name.setText(user.getUserName());` http://i.imgur.com/yICLHpK.png btw how to pass it like that? – JeraldPunx Aug 25 '13 at 07:23
  • 1
    You've got to create the User class somewhere. When you click "next", you could ask the editor panel for the User (ie editorPane.getUser()) and pass it to the display panel (ie displayPane.aetUser(user)) – MadProgrammer Aug 25 '13 at 07:41
  • still don't know how to do this.. can u put sample/view and put here sample... i will just upload my program to [mediafire](http://www.mediafire.com/?4badf1aa00nancz) and i am using netbeans... I just using the `StudentRegistrationForm_1.java` and `StudentRegistrationForm_3.java` – JeraldPunx Aug 25 '13 at 08:24
  • 1
    I've posted a basic working example. To frank, it doesn't go much beyond what I've already recommended, but you can at least step through the code with a debugger – MadProgrammer Aug 25 '13 at 09:23
  • do you know how to that on Jframe into another JFrame not a panel... cause it's too hard... sorry for being noob... – JeraldPunx Aug 25 '13 at 11:37
  • 1
    One frame talking to another is not different from one panel talking to another, or one object from talking to another. This is a basic principle and it doesn't change – MadProgrammer Aug 25 '13 at 11:42
  • mine not working... can you view my code [link](http://www.mediafire.com/?4badf1aa00nancz) i've done that on netbeans.. just test the `TexUsername` on `StudentRegistrationForm_1.java` to `name` on `StudentRegistrationForm_3` still hoping to this.. so I can figure it out – JeraldPunx Aug 25 '13 at 11:55
  • 1
    If you want `StudentRegistrationForm_3` to use the `User` object, you need to pass it to it, either via the constructor or via a `setter` method... – MadProgrammer Aug 25 '13 at 12:03
  • here this is my code http://pastie.org/8268066 i removed the design code cause it's not editable... can you give me some sample? still not figuring it out... tuesday is our deadline of school project... and i'm still on register thing – JeraldPunx Aug 25 '13 at 12:28
  • 1
    Understand, that I'm not going to do your homework for you. I've added some additional suggestions based on the code you have provide, all of which repeat what I've being saying all along. – MadProgrammer Aug 25 '13 at 23:07
  • can I ask 1 more last sample using 2 jframe... not a panel... please.. this is last... sorry for making u mad and thanks to all your effort.. – JeraldPunx Aug 25 '13 at 23:35
  • 1
    And I'm not mad, I want you to understand why I'm not giving your a direct, runnable example ;) – MadProgrammer Aug 25 '13 at 23:38
  • i created the try and catch to know the error... and the error i got is `java.lang.NullPointerException` and on `StudentRegistrationForm_3` i put this constructer... i don't know if this right or wrong... `StudentRegistrationForm_3(User user) { name.setText(user.getName()); }` – JeraldPunx Aug 26 '13 at 00:15
  • 1
    Make sure that you are passing something useful to the constructor (ie User is not null), the name label is not null (not being called before initComponent) – MadProgrammer Aug 26 '13 at 00:18
  • I edited my question... still giving me error just look at the code if i'm doing right... on below of **NEW UPDATES ON QUESTION** – JeraldPunx Aug 26 '13 at 00:47
  • 1
    It would suggest that name is null – MadProgrammer Aug 26 '13 at 01:49
  • how to do that?... i add text on my jlabel named `name` – JeraldPunx Aug 26 '13 at 02:24
  • 1
    Have you initialised it? – MadProgrammer Aug 26 '13 at 03:56
  • hmmm... yeah?? where do I initialised it? you mean this? `User user = new User(TexUsername.getText(),Password.getPassword()); StudentRegistrationForm_3 form = new StudentRegistrationForm_3(user); form.setUser(user); this.dispose();` – JeraldPunx Aug 26 '13 at 05:02
  • 1
    No, I mean the label. It should be initialised in StudentRegistrationForm3 BEFORE you try and set its text – MadProgrammer Aug 26 '13 at 05:31
  • you mean like this... `name.setText("asd");` or ` name = new javax.swing.JLabel();` cause it's already on the code... – JeraldPunx Aug 26 '13 at 05:38
  • 1
    Okay, what about User, is it null when you pass it to the form? To is where a debugger is invaluable – MadProgrammer Aug 26 '13 at 07:19
  • still not... i created new like this ... `String user1 = "AW"; char[] pass1 = {'a'};` and inside my next button is `user1 = TexUsername.getText(); pass1 = Password.getPassword(); User user = new User(user1,pass1); StudentRegistrationForm_3 form = new StudentRegistrationForm_3(user); form.setUser(user); this.dispose();` – JeraldPunx Aug 26 '13 at 08:18
  • 1
    In you `StudentRegistrationForm_3(User user)` constructor, you are not initialising the UI components. Try adding `this();` as the first statement in the constructor, following by `name.setText(user.getName());` – MadProgrammer Aug 26 '13 at 08:54
  • I added `this();` and when I click next... it doesn't open `StudentRegistrationForm_3.java` but I can't see error all I can see is **Build Successful** and when I remove `this();` still null error... idk why... – JeraldPunx Aug 26 '13 at 10:18
  • 1
    I don't see a `setVisible(true)` for the `StudentRegistrationForm_3` class – MadProgrammer Aug 26 '13 at 11:37
  • oh yeahhhhhhhhh finallyyyy Thanks for the HELPPPPPPPPP... I'm so happpppyyyyy...now I understand this passing technique... you're so cool master!!! after this project I will master this java and become like you... hahaahah – JeraldPunx Aug 26 '13 at 12:27