Solved

Neter password in jPassword field with number jButtons

Posted on 2011-03-02
6
976 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:BinaryFlusher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35023115
button = new JButton("a");
button.setActionCommand("1");
button.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent event) {
      passwordField.setText(passwordField.getText()+event.getActionCommand());
   }
});
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35023134
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

0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 35023201
sorry a small typo in the above. should of course be


button = new JButton("1");
button.setActionCommand("1");
button.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent event) {
      passwordField.setText(passwordField.getText()+event.getActionCommand());
   }
});

Do the same for the other buttons
or use a loop

for (int i=0; i<10; i++) {
   String d = Integer.toString(i);   // convert int to string
   JButton button = new JButton(d);
   button.setActionCommand(d);
   button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
         passwordField.setText(passwordField.getText()+event.getActionCommand());
      }
   });
   panel.add(button);
}
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 86

Expert Comment

by:CEHJ
ID: 35023269
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

0
 

Author Comment

by:BinaryFlusher
ID: 35025575
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());
   }
});
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 35025607
> Your code below appears to use a string for the setActionCommand value, should this not be a char

no its a string

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

yes it uses characters to retrieve the password, so change what I posted to

      passwordField.setText(new String(passwordField.getPassword())+event.getActionCommand());
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question