KM529
KM529

Reputation: 402

Variable won't take data from JTextField

I'm fairly new to coding and am working on getting a cheap and easy log-in code to work by checking first if the index file exists(has the password in it), and if so, reading what it says and asking for the username and password from the user. If the file doesn't exist or is empty, it should prompt the user for the two fields to be used from then on and write them to the index file.

The Issue I have run into, however, is that the button I have set up to run the code to write those two fields to the file does not seem to find anything in the Strings and as such can't write anything. I included the code below and can add more of it if there is anything i forgot.

Below is the code for the GUI window and the action listeners.

public class GUIComponents extends JFrame{
public static String UserN = " ";
public static String PassW = " ";

public static void LoginScreen(){
    JFrame LoginFrame = new JFrame("Log in Menu");
    JPanel LoginInfo = new JPanel();
    JPanel LoginAttempt = new JPanel();

    JButton LoginOkay = new JButton("Log in");
    LoginListenerClass loginListen = new LoginListenerClass();
    LoginOkay.addActionListener(loginListen);
    JButton LoginCancel = new JButton("Cancel");
    CancelListenerClass cancelListen = new CancelListenerClass();
    LoginCancel.addActionListener(cancelListen);
    JLabel usern = new JLabel("Username:");
    JLabel passw = new JLabel("Password:");
    JTextField user = new JTextField(15);
    JTextField pass = new JTextField(15);
    LayoutManager LoginLayout = new GridLayout(2,2,5,10);
    LayoutManager OkayCancel = new FlowLayout();
    LoginInfo.setLayout(LoginLayout);
    LoginAttempt.setLayout(OkayCancel);

    LoginInfo.add(usern);
    LoginInfo.add(user);
    LoginInfo.add(passw);
    LoginInfo.add(pass);
    LoginAttempt.add(LoginOkay);
    LoginAttempt.add(LoginCancel);

    if(Login.firstTimeCheck()==true){
        JOptionPane.showMessageDialog(null, "This is your first time using this program. \nPlease enter the Username"
                + " and Password\n              you wish to set for all users.");
    }
    LoginFrame.add(LoginInfo, BorderLayout.CENTER);
    LoginFrame.add(LoginAttempt, BorderLayout.SOUTH);

    LoginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    LoginFrame.setLocationRelativeTo(null);                         




    LoginFrame.pack();
    LoginFrame.setVisible(true);

    UserN = user.getText();
    PassW = pass.getText();
}

}

class LoginListenerClass implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        if(Login.firstTimeCheck()==false){
            Login.passCheck();
        }
        else{
            try {
                Login.CreateLogin(GUIComponents.UserN,     GUIComponents.PassW);
                System.out.println(GUIComponents.UserN+" "+ GUIComponents.PassW);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
}

class CancelListenerClass implements ActionListener{
        @Override
    public void actionPerformed(ActionEvent e){
        System.exit(0);
    }
}

Below this is the code for the Log-in functions:

public class Login {
private static String UserName;     //true username
private static String Password;     //true password
private static boolean accepted = false;
private static String user="";
private static String pass="";
private static boolean firstTime = true;

Login(){

}

public static void CreateLogin(String user, String pass) throws IOException{
    UserName = user;
    Password = pass;

    try{
        FileWriter FW = new FileWriter("Index.txt");
        PrintWriter output = new PrintWriter(FW);
        output.println(UserName);
        output.println(Password);
        output.close();
        FW.close();
    }
    catch(IOException e){
        e.printStackTrace();
    }
}

public static void LogIn(){
    try{
        FileReader FR = new FileReader("Index.txt");
        firstTime = false;
        Scanner sc = new Scanner(FR);
        user = sc.nextLine();
        pass = sc.nextLine();
        if(user.equalsIgnoreCase("")){
            firstTime = true;
        }
        if(pass.equals("")){
            firstTime = true;
        }
    }

    catch(FileNotFoundException e){
        firstTime = true;
    }

    GUIComponents.LoginScreen();





}

public static void passCheck(){
    if(UserName.equalsIgnoreCase(user)){
        if(Password.equals(pass)){
            accepted = true;
        }
    }
    else{
        JOptionPane.showMessageDialog(null,"Your Username and Password do not /n match our files."
                + "Please try again.");
    }
}

public static boolean firstTimeCheck(){
    return firstTime;
}

}

If anyone can figure out what stupid thing I did in this case, please let me know. I apologize if the code is long and irritating to read.

Upvotes: 1

Views: 118

Answers (3)

Makky
Makky

Reputation: 17463

The variables you've defined

public static String UserN = " ";
public static String PassW = " ";

will not get the latest value from the text fields.

Proposed solution (not Ideal but for something your code)

  1. Make Jtext fields static public as in GuiComponents class

    public static JTextField user;    
    public static JTextField pass;
    

In your LoginListener

Login.CreateLogin(GUIComponents.user.getText(),GUIComponents.pass.getText());
System.out.println(GUIComponents.user.getText() + GUIComponents.pass.getText());

Upvotes: 2

pulasthi7
pulasthi7

Reputation: 901

Read the UserN, and PassW values within the listener class actionPerformed method. Otherwise you are reading the value at the initialization which is an empty string.

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

In your code here:

public static void LoginScreen(){
    // ............ code deleted to simplify things

    JTextField user = new JTextField(15);
    JTextField pass = new JTextField(15);

    // ..... code removed to simplify things

    LoginFrame.pack();
    LoginFrame.setVisible(true);

    UserN = user.getText();
    PassW = pass.getText();
}

You're trying to extract text from a JTextField on creation of the GUI and before the user has had a chance to enter anything into it. To solve this, don't extract the text on GUI creation, but rather due to an event such as in the ActionListener of a JButton which will be activated when the JButton is pressed, or the ActionListener of your JTextFields which will be activated when Enter is pressed when the text field has focus.


As an aside, your code suffers from gross over-use of static modifiers and suggests that you will want to learn how to create true OOP classes, and Java basics before trying to create Swing GUI's. You won't regret doing this.

Upvotes: 2

Related Questions