How do i get the password field to be equal to the password 123456 and for the button "ok" to work with it in java

80 Views Asked by At

I want when I click the button "edit, add and update programme", The password field comes up and the button "ok" comes up. The ok button is supposed to call the displaytable() method which will display an editable table for the user to change. how do i get the password to work

JButton passcode = new JButton("Edit, add or update programme");//creates button to display editable table
    passcode.setBounds(50,200,95,30);
        passcode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) { //here
                Char[] password = "123456";
                JButton button = new JButton("OK");
                JPasswordField pf = new JPasswordField();
                button.addActionListener(e1 -> {

                    if(JPasswordField.getPassword.equals(password){
                        panel1.add(pf);
                        panel1.add(button); //TO HERE IM HAVING A PROBLEM WITH
                    }
                });
            }
        });
1

There are 1 best solutions below

1
ControlAltDel On

A better design would be to add these Components initially but disable them (setEnabled(false)). Anyway, to answer your question in the scenario you have, you should run your code on the UI thread, and revalidate and repaint after adding the new components

                if(JPasswordField.getPassword().equals(password){
                   SwingUtilities.invokeLater(new Runnable() {
                      public void run() {
                        panel1.add(pf);
                        panel1.add(button); //TO HERE IM HAVING A PROBLEM WITH
                        panel1.validate();
                        panel1.repaint();
                     }
                  });
                }