Java Program That Reveals Hidden Password

856 views Asked by At

I want to make a simple Java program that reveals a password copied from a browser or another application that is hidden in the form of asterisks.

I wrote some code but it doesn't work!

When I copy the password from Facebook password field for example, and paste it to the JPasswordField on my program. And then transform it to normal text, it shows me wrong output (in fact, it gives me my class name which is weird)

However, when I copy any normal text and paste it into the JPasswordField and then get the text, it gives the correct normal text back!

So why isn't it working? I'm sorry if my question seems stupid a little. I'm new to programming and especially with practical programs. How can I make it work? :D

Thanks in advance, Here's the code:

class PasswordViewer extends JFrame {
    JPasswordField field = new JPasswordField(20);
    JButton btn = new JButton("OK");
    JPanel panel = new JPanel();
    char[] s;

    PasswordViewer() {
        setSize(300, 300);
        panel.add(field);
        panel.add(btn);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(panel);

        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                s = field.getPassword();
                findPass();
            }
        });
    }
    void findPass() {
        String str = "";
        for (int i = 0; i < s.length; i++) {
            str += s[i];
        }
        JOptionPane.showMessageDialog(null, str, "PasswordViewer",
            JOptionPane.INFORMATION_MESSAGE);
    }
    public static void main(String[] args) {
        PasswordViewer passview = new PasswordViewer();
        passview.setVisible(true);
    }
}
1

There are 1 answers

0
AudioBubble On BEST ANSWER

Apparently, you cannot copy from a browser's password field as @ankitkatiyar91 pointed out. I was mistaken. This question is meaningless until you can find a way to copy the password from the browser's password field.