Link to home
Start Free TrialLog in
Avatar of BinaryFlusher
BinaryFlusher

asked on

Neter password in jPassword field with number jButtons

Hi

Can anyone please give me a code example showing how I can enter a password in a jPasswrod field if I have numeric jButtons (0-9) from which the user will enter the PIN.

Can you please advise how and where I should place actionlisteners....

Thanks
Avatar of Mick Barry
Mick Barry
Flag of Australia image

button = new JButton("a");
button.setActionCommand("1");
button.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent event) {
      passwordField.setText(passwordField.getText()+event.getActionCommand());
   }
});
To each button, add an instance the following, e.g

b.setAction(new PasswordFieldAction(digit));
private class PasswordNumberAction extends AbstractAction {
	public PasswordNumberAction(String digit) {
	    super(digit);
	}

	public void actionPerformed(ActionEvent e) {
	    passwordField.setText(passwordField.getText() + e.getActionCommand());
	}
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is the sort of thing you can do
import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;


public class F extends JFrame {
    JPasswordField passwordField;

    private void setGui() {
        try {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Container cp = getContentPane();
            passwordField = new JPasswordField();
            cp.add(passwordField, BorderLayout.NORTH);
            cp.add(getNumberPad(), BorderLayout.CENTER);
            cp.add(new JButton(new PasswordRevealer("Reveal")),
                BorderLayout.SOUTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public JPanel getNumberPad() {
        final int ROWS = 4;
        final int COLS = 3;

        JPanel p = new JPanel();
        p.setLayout(new GridLayout(ROWS, COLS));

        for (int i = 0; i < (ROWS * COLS); i++) {
            if (i < 10) {
                p.add(new JButton(new PasswordNumberAction("" + i)));
            } else {
                p.add(new JPanel());
            }
        }

        return p;
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        F f = new F();
                        f.setGui();
                        f.setSize(200, 200);
                        f.setVisible(true);
                    }
                });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class PasswordRevealer extends AbstractAction {
        public PasswordRevealer(String caption) {
            super(caption);
        }

        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(F.this,
                new String(passwordField.getPassword()));
        }
    }

    private class PasswordNumberAction extends AbstractAction {
        public PasswordNumberAction(String digit) {
            super(digit);
        }

        public void actionPerformed(ActionEvent e) {
            passwordField.setText(passwordField.getText() +
                e.getActionCommand());
        }
    }
}

Open in new window

Avatar of BinaryFlusher
BinaryFlusher

ASKER

hi objects

Your code below appears to use a string for the setActionCommand value, should this not be a char

When I reviewed the jPasswordField API it talks about the field being constructed from chars...?


button = new JButton("1");
button.setActionCommand("1");
button.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent event) {
      passwordField.setText(passwordField.getText()+event.getActionCommand());
   }
});
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial