Now I know that this is a bad approach, but I'm making a program and i need users to log in. I thought that if I created two different JFrames (one to log in, and one for my program) that I could just change their respective visibility states to decide which frame I wanted to show.
I created the login screen and when you hit login, it checks the credentials and it's supposed to hide the login screen and show the main program's screen.
I tried disposing the login screen, and changing the visibility state of the screen separately and I keep getting a null pointer Exception. The program does continue to run but I don't think it's good to proceed with this error.
I'm not sure how I could get rid of the login screen. This is a basic sketch of my program and the three classes so far:
import java.awt.EventQueue;
public class Class1 {
public static void main(String args[]){
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Windows window = new Windows(); // localizes the window class
window.dMMWindow();
window.displayloginWindow(); //calls for the window method inside of the window class
} catch (Exception e) { //catches any exceptions
e.printStackTrace();
}
}
});
}
}
and the second class
import java.awt.Button;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import org.eclipse.wb.swing.FocusTraversalOnArray;
public class Windows {
private JFrame loginWindow;
private JFrame ErrorWindow;
private JFrame moneyManagerWindow;
private JTextField inputUsername;
private JTextField inputPassword;
/**
* Displays login window
*/
public void displayloginWindow(){
createLoginWindow();
loginWindow.setVisible(true);
}
public void disposeWindow(String frameName){ //disposes specified windows
switch (frameName){ //switch statement to close specified windows
case "loginWindow":
loginWindow.setVisible(false);
break;
case "ErrorWindow":
ErrorWindow.setVisible(false);
break;
case "moneyManagerWindow":
moneyManagerWindow.setVisible(false);
break;
default:
break;
}
}
public void displayErrorWindow(){
createErrorWindow();
ErrorWindow.setVisible(true);
}
public void dMMWindow(){
createMoneyManagerWindow();
moneyManagerWindow.setVisible(true);
}
private void createMoneyManagerWindow() {
moneyManagerWindow = new JFrame();
moneyManagerWindow.setResizable(false);
moneyManagerWindow.setTitle("Money Manager");
moneyManagerWindow.setBounds(100, 100, 621, 490);
moneyManagerWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
moneyManagerWindow.setLocationRelativeTo(null);
JTextPane welcomePane = new JTextPane();
welcomePane.setEditable(false);
welcomePane.setBackground(SystemColor.menu);
welcomePane.setText("Welcome: ");
welcomePane.setBounds(12, 13, 230, 22);
moneyManagerWindow.getContentPane().add(welcomePane);
}
private void createErrorWindow() {
ErrorWindow = new JFrame();
ErrorWindow.setTitle("Error");
ErrorWindow.setResizable(false);
ErrorWindow.setBounds(100, 100, 353, 193);
ErrorWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
ErrorWindow.setLocationRelativeTo(null);
ErrorWindow.setAlwaysOnTop(true);
JTextPane txtpnIncorrectUsernameOr = new JTextPane();
txtpnIncorrectUsernameOr.setBackground(SystemColor.menu);
txtpnIncorrectUsernameOr.setEditable(false);
txtpnIncorrectUsernameOr.setBounds(80, 30, 207, 22);
txtpnIncorrectUsernameOr.setText("Incorrect username or password");
JButton contButton = new JButton("Continue");
contButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ErrorWindow.dispose();
}
});
contButton.setBounds(127, 79, 97, 45);
ErrorWindow.getContentPane().setLayout(null);
ErrorWindow.getContentPane().add(txtpnIncorrectUsernameOr);
ErrorWindow.getContentPane().add(contButton);
}
/**
* Initialize the contents of the frame.
*/
private void createLoginWindow() {
loginWindow = new JFrame();
loginWindow.setResizable(false);
loginWindow.setTitle("Money Planner");
loginWindow.setBounds(100, 100, 450, 234);
loginWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
loginWindow.setLocationRelativeTo(null);
loginWindow.getContentPane().setLayout(null);
loginWindow.isAlwaysOnTop();
Button cAccButton = new Button("Create Account");
cAccButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//when button is clicked
}
});
cAccButton.setBounds(100, 151, 104, 24);
loginWindow.getContentPane().add(cAccButton);
Button loginButton = new Button("Login");
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String username = inputUsername.getText(); // sets username equal to whatever was in the username text field
String password = inputPassword.getText(); // sets password equal to whatever was in the password text field
Users users = new Users(); //localizes the users class
try{
users.analyze(username, password); // sends the local variables "username" and "password" over to the analyze method in the Uers class
}
catch (Exception e){
e.printStackTrace();
}
}
});
loginButton.setBounds(232, 151, 104, 24);
loginWindow.getContentPane().add(loginButton);
inputUsername = new JTextField();
inputUsername.setBounds(180, 61, 116, 22);
loginWindow.getContentPane().add(inputUsername);
inputUsername.setColumns(10);
inputPassword = new JTextField();
inputPassword.setBounds(180, 96, 116, 22);
loginWindow.getContentPane().add(inputPassword);
inputPassword.setColumns(10);
loginWindow.getContentPane().setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{inputUsername, inputPassword, cAccButton, loginButton}));
loginWindow.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{inputUsername, inputPassword, loginButton, cAccButton}));
}
}
And the last class
public class Users {
private String username = "q"; //created random username for debugging
private String password = "1"; //created random password for debugging
public void analyze(String username, String password){ //accepts username and password
Windows window = new Windows();
//System.out.println("username :" + username + " password: " + password); //debugging
if (this.username.equals(username)){ //tests to see if the classes username equals the inputed username
if (this.password.equals(password)){ //tests to see if the classespassword equals the inputed password
window.dMMWindow();
window.disposeWindow("loginWindow");
//what to do once logged in
//function once botch username and password match
} else {
window.displayErrorWindow(); // output for incorrect information
}
} else if (!this.username.equals(username)){ //not sure if I need this but i'll keep it
window.displayErrorWindow();
}
}
}