I am currently taking java and learning how to create GUI. I am trying to create a GUI whereby

1) Upon executing the application,the user will need to key in his username and password.
2) the user will be brought to second page which displays "Welcome to Page Two!" if successfully log in.

  1. I declared a boolean variable(isLogin) to check whether the user has enter a valid username and password in my JButtton in LoginPageUI.java.

  2. I also create a method verifyLogin to return the boolean(isLogin) whether is true or false.

  3. In my main class(RunApp.java) I use the verifyLogin method to check whether is true or false but however it's seems that it will always return false even though I have typed in the correct username and password("a" and "123").

How do I resolve this problem?

LoginPageUI.java

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.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class LoginPageUI {

private boolean isLogin; 

public LoginPageUI() {

}

public JPanel LoginUI () {
        JPanel loginPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        JLabel usernameLabel = new JLabel("username");
        JLabel passwordLabel = new JLabel("password");
        final JTextField usernameTF = new JTextField(20);
        final JTextField passwordTF = new JTextField(20);
        JButton loginBtn = new JButton("Login");    

        c.gridx = 0;
        c.gridy = 1;
        loginPanel.add(usernameLabel,c);

        c.gridx = 1;
        c.gridy = 1;
        loginPanel.add(usernameTF,c);

        c.gridx = 0;
        c.gridy = 2;
        loginPanel.add(passwordLabel,c);

        c.gridx = 1;
        c.gridy = 2;
        loginPanel.add(passwordTF,c);

        c.gridx = 1;
        c.gridy = 3;
        loginPanel.add(loginBtn,c);

        loginBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String u = usernameTF.getText().toString();
                String p = passwordTF.getText().toString();
                if(u.equals("a") && p.equals("123")) {
                    isLogin = true;
                }
            }
        });
        return loginPanel;
    }

    public boolean verifyLogin() {
        return isLogin;
    }
}

PageTwo.java

import javax.swing.JLabel;
import javax.swing.JPanel;


public class PageTwo {

public PageTwo() {

}

public JPanel displayPageTwo() {
     JLabel label = new JLabel("Welcome to Page Two!");
     JPanel pTwoPanel = new JPanel();
     pTwoPanel.add(label);
     return pTwoPanel;
}

RunApp.java

import java.awt.CardLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class RunApp {

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

public RunApp() {
 SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            CardLayout cardLayout = new CardLayout();
            JFrame frame = new JFrame("testing");
            JPanel panel = new JPanel();
            panel.setLayout(cardLayout);
            LoginPageUI lPage = new LoginPageUI();
            PageTwo pageTwo = new PageTwo();
            panel.add(lPage.LoginUI(),"1");
            panel.add(pageTwo.displayPageTwo(),"2");

            cardLayout.show(panel, "1");

            if(lPage.verifyLogin() == true) {
                   cardLayout.show(panel, "2");
            }

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(panel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

I am using eclipse

The problem is in the sequence of events...
You check the inputs and set the boolean when the user clicks the button, but you check the value as part of your initialisation (line 27), which is before the user could possibly have entered anything or clicked the button.
You will have to do a bit of re-structuring so that the code in your action listener switches to panel 2 when the button is clicked and the inputs are correct.

Right I got what you mean. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.