0

I created 2 jframes using netbeans. One is named as login and the other is home. I inserted a textfield at login frame to insert username. I also placed a label at home frame to display the username entered in login frame textfield. To do this i created global variable to get text from username textfield at login frame.

public class Login extends javax.swing.JFrame {
public static String user;

user = txt_user.getText();

Then i created an instance of login class in home class to call this variable and set text to the label i used.

public Home() {
    initComponents();
    date();
    time();

    Login l = new Login();
    lbl_user.setText(l.user);

There were no errors. But it's not working for me. Please help me on this. I also want to know what the return type is of the following method.

public getuser(){
String u = txt_user.getText();
return;
}

So any help on this matter is appreciated. Thanks in advance.

  • 1
    First see [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556); Second I think you actually want a modal dialog, see [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html); third, consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Mar 10 '15 at 02:42
  • Thanks, I appreciate that. I'll keep that in mind the next time. – Nalaka Chamod Mar 10 '15 at 02:56
  • And let's not even get started about the [evils of `static`](http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil) – MadProgrammer Mar 10 '15 at 02:57

2 Answers2

2

It's well worth your effort to learn to hand code your UI's it will make it easier to know when to use a form editor and how to get the most out of them.

You should restrict the number of windows you dump on the user to one active window and make use dialogs to show or prompt the user for more information. See How to Make Dialogs for more details

JFrame is not a blocking window, that is, when you make it visible, the code will continue executing, meaning, in your case, you show the login window, but the user hasn't had time to enter anything before you try and set the label's text...

This is where a modal dialog comes in...

LoginPanel loginPane = new LoginPanel();
// This will wait here until the dialog is closed for some reason...
int result = JOptionPane.showConfirmDialog(this, loginPane, "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
// Then you can determine what you want to do in response to the
// closing of the dialog
if (result == JOptionPane.OK_OPTION) {
    label.setText("Welcome " + loginPane.getUsername());
}

Runnable example...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        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);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {

            setLayout(new GridBagLayout());
            JLabel label = new JLabel("Unknown user");
            add(label);

            LoginPanel loginPane = new LoginPanel();
            int result = JOptionPane.showConfirmDialog(this, loginPane, "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            if (result == JOptionPane.OK_OPTION) {
                label.setText("Welcome " + loginPane.getUsername());
            }

        }

    }

    public class LoginPanel extends JPanel {

        private JTextField userName;
        private JPasswordField password;

        public LoginPanel() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;

            add(new JLabel("Username: "), gbc);
            gbc.gridy++;
            add(new JLabel("Password: "), gbc);

            gbc.gridx++;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.EAST;

            userName = new JTextField(20);
            password = new JPasswordField(20);

            add(userName, gbc);
            gbc.gridy++;
            add(password, gbc);
        }

        public String getUsername() {
            return userName.getText();
        }

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

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

You need to take care when to use static variables. In this case, you will not need a static, and inseat of use a JFrame to get the Login, you can use:

Home() {
    initComponents();

    String login = JOptionPane.showInputDialog(this, "Input your name");
}
fabiojb
  • 331
  • 2
  • 10