Home » Java » java – How do I refresh the information from text fields in this loop?-Exceptionshub

java – How do I refresh the information from text fields in this loop?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am trying to make a login GUI, which closes when a correct combination of username and password are entered. When I enter them correctly, it works fine, but if an incorrect combination is entered, the loop just keeps repeating with the same values of “u” and “pw”, stopping me from typing anything else.

I tried adding a logB.setSelected(false); at the end of the while loop, but it still doesn’t let me enter new values.

(BTW I know that the incorrect password label shows up from the beginning, but I’m just trying to solve this problem right now. Also the Login class started off as CLI, so there are some remnants of that left in the code, but I just want to get it working before I clean it up.)

GUI class:

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Login_GUI implements ActionListener {


private static JLabel userL;
private static JTextField userT;
private static JLabel pwL;
private static JPasswordField pwT;
private static JLabel respL;
private static JFrame f; 
public static boolean authorized;
public static void GUIfunc(){
    JPanel p = new JPanel();
    f = new JFrame("RMT 1.0");
    f.setSize(250, 225);
    f.setLocation(1080,530);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    f.add(p);

    userL = new JLabel("Username:");
    userL.setBounds(10,20,80,25);
    p.add(userL);
    userT = new JTextField(20);
    userT.setBounds(100,20,165,25);
    p.add(userT);

    pwL = new JLabel("Password:");
    pwL.setBounds(10,50,80,25);
    p.add(pwL);
    pwT = new JPasswordField(20);
    pwT.setBounds(100,50,165,25);
    p.add(pwT);

    JButton logB = new JButton("Login");
    logB.setBounds(10,80,80,25);
    logB.addActionListener(new Login_GUI());
    p.add(logB);

    respL = new JLabel("Incorrect username or password.");
    respL.setBounds(10,100,80,25);
    respL.setEnabled(false);
    p.add(respL);

}
@Override
    public void actionPerformed(ActionEvent e){
    while (authorized != true){
        String u = userT.getText();
        String pw = pwT.getText();
        try {
            authorized = Login2.loginfunc(u,pw);
        }
        catch(IOException exc){
            exc.printStackTrace();
        }
        logB.setSelected(false);

    }
    f.dispatchEvent(new WindowEvent(f, WindowEvent.WINDOW_CLOSING));

}
}

Login class:

import java.util.*;
import java.io.*;
import java.nio.*;

class Login2{
public static boolean loginfunc(String u, String pw)throws IOException{
    File users = new File("C:\Users\Marcell\OneDrive\Computer Science\IA\users.txt");
    Scanner kb = new Scanner (System.in);
    Scanner sc = new Scanner (users);

    String token1 = "";
    int numOfEmp = 3;
    boolean authorized = false;
    boolean repeat = true;  
    String temp[] = new String[numOfEmp];

    for(int x=0;x<numOfEmp;x++){         
       token1 = sc.nextLine();
       temp[x] = token1;
    }


    String full = u + "," + pw;
    for (int x=0;x<numOfEmp;x++ ){
        String pass = temp[x];
        if (pass.equals(full)){
             System.out.println("Login successful.");
             authorized = true;
             repeat = false;
            }              
    }
    if (repeat = false){
        return(false);
    }

    return(authorized);
}
}

Thanks for the help in advance!

How to&Answers:

Replace actionPerformed as follows:

public void actionPerformed(ActionEvent e){
    if (!authorized){
        String u = userT.getText();
        String pw = pwT.getText();
        try {
            authorized = Login2.loginfunc(u,pw);
            if(authorized) {
                f.dispatchEvent(new WindowEvent(f, WindowEvent.WINDOW_CLOSING));
            }
        }
        catch(IOException exc){
            exc.printStackTrace();
        }
        logB.setSelected(false);
    }       
}