public class PasswordField extends JFrame {
private JPanel passwordPanel;
private JPasswordField passwordField;
private JLabel passwordLabel;
private JButton userConfirmationBtn;
private JButton submenu;
private Image icon;
private ImageIcon frameBackground;
private ImageIcon buttonBackground;
private String passwordAuthentication;
public PasswordField() {
initComponents();
}
private void initComponents() {
icon = new ImageIcon("c:/pics/mercuryLogo.jpg").getImage().getScaledInstance(300, 300, 0);
frameBackground = new ImageIcon("c:/pics/passwordbck2.jpg");
buttonBackground = new ImageIcon("c:/pics/buttonbck");
passwordField = new JPasswordField();
passwordLabel = new JLabel("Password: ");
userConfirmationBtn = new JButton("Enter");
submenu = new JButton("Submenu");
// set the properties of this label
passwordLabel.setBounds(12, 13, 80, 20);
passwordLabel.setFont(new Font("Monospaced", Font.BOLD, 12));
passwordLabel.setForeground(Color.BLACK);
// set the properties and actions of this text field
passwordField.setBounds(88, 13, 185, 20);
// set the properties and actions of this button
Action pass = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
Password p = new Password();
// retrive the password data from the password field
char[] passwordChars = passwordField.getPassword();
String passwordStr = "";
// convert the returned password characters as string
for (int x = 0; x <= passwordChars.length - 1; x++) {
passwordStr = passwordStr + passwordChars[x];
}
// check if the password entered is valid
if (p.isPasswordAuthenticated(passwordStr)) {
// new InventoryDatabase();
disableWindow();
}
else if (passwordStr.equals("")) {
// PopUpDialog.popTheErrorMessage(" Unauthorized Entry", "Please Fill The Specified Field.");
}
else {
// PopUpDialog.popTheErrorMessage(" Unauthorized Entry !", "Access Denied.");
}
}
};
userConfirmationBtn.addActionListener(pass);
userConfirmationBtn.setMnemonic(KeyEvent.VK_E);
userConfirmationBtn.setBounds(88, 55, 90, 20);
userConfirmationBtn.setFont(new Font("Monospaced", Font.BOLD, 11));
userConfirmationBtn.setBackground(Color.LIGHT_GRAY);
userConfirmationBtn.registerKeyboardAction(userConfirmationBtn.getActionForKeyStroke(
KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
JComponent.WHEN_IN_FOCUSED_WINDOW);
userConfirmationBtn.registerKeyboardAction(userConfirmationBtn.getActionForKeyStroke(
KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
JComponent.WHEN_IN_FOCUSED_WINDOW);
// set the properties and actions of this button
Action openSubmenu = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
Submenu.openSubmenu();
}
};
submenu.addActionListener(openSubmenu);
submenu.setMnemonic(KeyEvent.VK_S);
submenu.setBounds(184, 55, 90, 20);
submenu.setFont(new Font("Monospaced", Font.BOLD, 11));
submenu.setBackground(Color.LIGHT_GRAY);
passwordPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
// Scale image to size of component
Dimension d = getSize();
g.drawImage(frameBackground.getImage(), 0, 0, d.width, d.height, null);
super.paintComponent(g);
}
};
passwordPanel.setOpaque(false);
// set this panel's layout to null for absolute positioning of
// components
passwordPanel.setLayout(null);
// add the components to this panel
passwordPanel.add(passwordLabel);
passwordPanel.add(passwordField);
passwordPanel.add(userConfirmationBtn);
passwordPanel.add(submenu);
// add the panel to the container
getContentPane().add(passwordPanel);
setTitle(" User Confirmation");
setIconImage(icon);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(288, 120);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
/**
* Invoke this method if this window doesnt have any more use for the current
* event.
*/
private void disableWindow() {
setVisible(false);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PasswordField();
}
});
}
}