How to implement password field which will enable or disable use of menu bar

211 views Asked by At

Basically what I would like to do is have a password field on my main menu jframe which will either allow a user to select from the menu bar on the jframe. If they do not get the password correct, they cannot select anything from any of the menu items in the menu bar, hence they will not be able to use the menu. I know how to implement a password field, but how do I connect it to use of the main menu bar? Much appreciation in advance. BTW, I have code written for the menu bar, but none for the password field yet, that is why I didn't post the code yet.

1

There are 1 answers

0
Gal Aharon On BEST ANSWER

You may use JPasswordField like this:

JFrame frame = new JFrame();
JMenuBar mb = new JMenugBar();
/* here you create all the menu*/ 
char[] password = "123456"; 
JButton button = new JButton("OK");
JPasswordField pf = new JPasswordField();
button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        if(JPasswordField.getPassword.equals(password){
            frame.add(mb);
        }
});
frame.add(pf);
frame.add(button);