How to mask input from a JFormattedTextField with asterisks?

I have a little java program fully working in Eclipse. I can ask a user to enter a pin. It works. But now I want to go to the next step and mask what he enters. I need the pin hidden when he is entering it. I guess it is possible to display asterisks ("*") in the JFormattedTextField when he is writing, right ? How can I do that ?

Here is my code :

package codePin;

import java.io.*;
import java.text.NumberFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame {

    private static final long serialVersionUID = 1L;

    private JPanel container = new JPanel();
    private JFormattedTextField jtf = new JFormattedTextField(NumberFormat.getIntegerInstance());
    private JLabel label = new JLabel("Enter Pin: ");
    private JButton b = new JButton("OK");


    public Main() {
        this.setTitle("NEEDS");
        this.setSize(300, 500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);

        container.setBackground(Color.white);
        container.setLayout(new BorderLayout());
        JPanel top = new JPanel();
        Font police = new Font("Arial", Font.BOLD, 14);
        jtf.setFont(police);
        jtf.setPreferredSize(new Dimension(100, 30));
        jtf.setForeground(Color.BLUE);

        b.addActionListener(new BoutonListener());

        top.add(label);
        top.add(jtf);
        top.add(b); 


        this.setContentPane(top);
        this.setVisible(true);
    }

    class BoutonListener implements ActionListener {
        private final AtomicInteger nbTry = new AtomicInteger(0);

        public void actionPerformed(ActionEvent e) {
            if (nbTry.get() > 2) {
                JOptionPane.showMessageDialog(null, "Pin blocked due to 3 wrong tries");
                return;
            }
            if (jtf.getText().replaceAll("\u00A0", "").length() != 4) {
                //System.out.println("Pin must be 4 digits");
                JOptionPane.showMessageDialog(null, "Ping must be 4 digits");
                return;
            }
            System.out.println("Checking...");
            SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
                @Override
                protected Void doInBackground() throws Exception {
                    boolean authenticated = false;
                    ArrayList<Integer> pins = new ArrayList<Integer>();
                    readPinsData(new File("bdd.txt"), pins);
                    String[] thePins = new String[pins.size()];
                    for (int i = 0; i < thePins.length; i++) {
                        thePins[i] = pins.get(i).toString();
                    }
                    String passEntered = String.valueOf(jtf);
                    for (String thePin : thePins) {
                        if (passEntered.equals(thePin) && jtf.getText().length() == 4) {
                            System.out.println(":)");
                            authenticated = true;
                            break;
                        }
                    }
                    if (!authenticated) {
                        System.out.println(":(");
                        nbTry.incrementAndGet();
                    }
                    return null;
                }
            };
            worker.execute();
        }


    }

    // Read/Access pins bdd.txt file
    static public boolean readPinsData(File dataFile, ArrayList<Integer> data) {
        boolean err = false;
        try {
            Scanner scanner = new Scanner(dataFile);
            String line;
            while (scanner.hasNext()) {
                line = scanner.nextLine();
                try {
                    data.add(Integer.parseInt(line));
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    err = true;
                }
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            err = true;
        }

        return err;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main();
            }
        });

    }
}

Any ideas ? Thank you.

Florent.

Answers


try JPasswordField like below,

JPasswordField p1=new JPasswordField("pass",6);
p1.setEchoChar('*');
top.add(p1);

Need Your Help

Django Templates: How to show the time differences between a Datastore time and current time?

django google-app-engine

I am working on a django- Google app engine project. A user inserts some value and the datetime field is auto filled using google app engine DateTimeProperty(auto_now_add=True) property. Now, I hav...

Get data by specific date to render in ListView

c# asp.net sql-server-2008

I'm trying to get blogposts by dates to build up an archive, more specifically I want the data to be presented by month of the year, e.g. July 2012, August 2012 and so on. And oh, consider me a new...