The program I’m designing allows users to change an admin password ONLY AFTER entering the current admin password into a password field in the class setupModeLock below. However, the password never saves to the setupModeLock variable pWord
import javax.swing.*;
import java.awt.event.*;
public class setupModeLock {
private static int pWord;
public final JFrame miniScreen;
setupModeLock() {
miniScreen = new JFrame("Setup Mode");
final JLabel info = new JLabel("Please enter an admin password");
final JPasswordField adminField = new JPasswordField();
final JLabel result = new JLabel("");
info.setBounds(120, 300, 200, 30);
adminField.setBounds(120, 325, 200, 30);
result.setBounds(200, 325, 400, 30);
miniScreen.add(info);
miniScreen.add(adminField);
miniScreen.add(result);
adminField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent code) {
String password = new String(adminField.getPassword());
try {
int val = Integer.parseInt(password);
if(val == pWord)
{
miniScreen.dispose();
new setUpMode()
}
else
{
adminField.setText("");
result.setText("Incorrect Password");
}
} catch (Exception e) {
adminField.setText("");
result.setText("Invalid Password");
}
}
});
miniScreen.setSize(780,700);
miniScreen.setVisible(true);
miniScreen.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void changePassword(int newPass)
{
pWord = newPass;
}
public static int getPword()
{
return pWord;
}
}
The next class actually displays the button that changes the password
import javax.swing.*;
import java.awt.event.*;
public class setUpMode extends setupModeLock{
public JFrame window;
setUpMode() {
super();
miniScreen.dispose();
window = new JFrame();
window.setAlwaysOnTop(true);
JButton modPassword = new JButton("Change Admin Password");
modPassword.setBounds(1000, 800, 300, 30);
window.add(modPassword);
modPassword.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent c) {
new PasswordChanger();
}
});
}
}
In the class PasswordChanger, the user enters the password to the PasswordField, and then uses the inherited method changePassword to change the password in setUpModeLock
import javax.swing.*;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PasswordChanger extends setupModeLock implements ActionListener {
JPasswordField thisField;
JLabel success;
Container container;
CardLayout cardlayout;
PasswordChanger() {
super();
miniScreen.dispose();
JFrame newPlace = new JFrame("Change Password");
container = newPlace.getContentPane();
cardlayout = new CardLayout();
container.setLayout(cardlayout);
success = new JLabel("");
thisField = new JPasswordField();
thisField.addActionListener(this);
container.add("a", thisField);
container.add("b", success);
newPlace.setSize(300,100);
newPlace.setVisible(true);
newPlace.setLocationRelativeTo(null);
newPlace.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent bro) {
val = new String(thisField.getPassword());
try
{
int pCode = Integer.parseInt(val);
changePassword(pCode);
success.setText("Password successfuly save. Exit now");
} catch(Exception e) {
success.setText("Faulty Password type. Exit and try again");
}
cardlayout.next(container);
}
}
But after running it, not even the Change Password JButton appears in setupMode.
I tried inheriting over multiple classes, but my password always remains at 0. What can I do to modify the password in setUpModeLock so that any call to changePassword() will make the new password a constant in all subclasses.