Login form. Basically it would ask the user enter a username and password and compare it to a list of username and passwords in users.txt , if it finds the correct username and password the form will changes to "login Successful".
Just for testing purposes, i thought it would be interesting to see the value of usernameField.getText(), but problem is regardless how i debug the program i will not reach these two line of code:
String inputUsername = usernameField.getText().toLowerCase();
String inputPassword = passwordField.getText();
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class LoginFrame extends JFrame {
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 200;
private HashMap<string, string=""> usernamesAndPasswords;
private JTextField usernameField;
private JTextField passwordField;
private JButton loginButton;
private JPanel loginPanel;
private JPanel welcomePanel;
private JPanel mainPanel;
private CardLayout cardLayout;
public LoginFrame() {
this.createComponents();
super.setTitle("Login Panel");
super.setSize(FRAME_WIDTH, FRAME_HEIGHT);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setVisible(true);
}
private void createComponents() {
this.mainPanel = new JPanel(new CardLayout());
this.createHashMap();
this.loginPanel = this.createLoginPanel();
this.welcomePanel = this.createWelcomePanel();
this.mainPanel.add(this.loginPanel, "LoginPanel");
this.mainPanel.add(this.welcomePanel, "WelcomePanel");
this.cardLayout = (CardLayout) this.mainPanel.getLayout();
this.cardLayout.show(this.mainPanel, "LoginPanel");
super.add(this.mainPanel);
}
private JPanel createLoginPanel() {
JPanel panel = new JPanel(new GridLayout(3, 2));
this.loginButton = new JButton("Login");
final int TEXT_FIELD_SIZE = 10;
this.usernameField = new JTextField(TEXT_FIELD_SIZE);
this.passwordField = new JPasswordField(TEXT_FIELD_SIZE);
this.loginButton.addActionListener(new ActionListener() {
private int loginAttempts = 3;
@Override
public void actionPerformed(ActionEvent arg0) {
boolean loggedIn = false;
String inputUsername = usernameField.getText().toLowerCase();
String inputPassword = passwordField.getText();
if (this.loginAttempts == 1) {
JOptionPane.showMessageDialog(null, "Number of login attemtps exceeded. Exitting...");
System.exit(1);
}
for (Map.Entry<string, string=""> validUser : usernamesAndPasswords.entrySet()) {
if (inputUsername.equals(validUser.getKey().toLowerCase())) {
if (inputPassword.equals(validUser.getValue())) {
System.out.println("Login successful!");
cardLayout.show(mainPanel, "WelcomePanel");
loggedIn = true;
}
}
}
if (!loggedIn) {
this.loginAttempts -= 1;
String message = String.format("Invalid username/password. %d %s remaining", this.loginAttempts,
(this.loginAttempts > 1) ? "attempts" : "attempt");
JOptionPane.showMessageDialog(null, message, "LOGIN FAILED", JOptionPane.INFORMATION_MESSAGE);
}
}
});
panel.add(new JLabel("Username"));
panel.add(this.usernameField);
panel.add(new JLabel("Password"));
panel.add(this.passwordField);
panel.add(this.loginButton);
return panel;
}
private JPanel createWelcomePanel() {
JPanel panel = new JPanel(new GridLayout(3, 1));
panel.add(new JLabel("Welcome"));
panel.add(new JButton("Change password"));
JButton logoutBtn = new JButton("Logout");
logoutBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
panel.add(logoutBtn);
return panel;
}
private void createHashMap() {
this.usernamesAndPasswords = new HashMap<string, string="">();
try {
Scanner fileScanner = new Scanner(new File("users.txt"));
while (fileScanner.hasNextLine()) {
String[] line = fileScanner.nextLine().split(" ");
this.usernamesAndPasswords.put(line[0], line[1]);
System.out.println(line[0] + " " + line[1]);
}
fileScanner.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null,
"Error: no users.txt file found. No users/passwords available to read.", "USERS.TXT NOT FOUND!",
JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
public static void main(String[] args) {
JFrame testFramee = new LoginFrame();
}
}
What I have tried:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------