Como limitar o número de dígitos em um JPasswordField em Java?
Eu tenho meu código Java trabalhando no Eclipse, mas preciso adicionar algumas funcionalidades.
Primeiro, como limitar o número de dígitos que podem ser inseridos pelo usuário? Na verdade, eu tenho um JPasswordField que permite que uma pessoa insira um código PIN, e eu gostaria que esse JPasswordField estivesse limitado a 4 dígitos no máximo. Então, como parar a entrada assim que 4 dígitos são inseridos?
Então, como posso jogar com o tamanho da caixa JPassword? Existe uma maneira de modificá-lo como um JTextField, por exemplo? Porque minha linha "p1.setPreferredSize (new Dimension (100, 25));" realmente não me permite modificar o tamanho da caixa.
Como você pode ver, a caixa JPassworldField tem um tamanho padrão que não consigo descobrir como modificar facilmente.
Aqui está o meu código:
package codePin;
import java.io.*;
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 JPasswordField p1 = new JPasswordField(4);
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();
p1.setPreferredSize(new Dimension(100, 25)); //How to really modifiy this ?
b.addActionListener(new BoutonListener());
top.add(label);
top.add(p1);
p1.setEchoChar('*');
top.add(b);
this.setContentPane(top);
this.setVisible(true);
}
class BoutonListener implements ActionListener {
private final AtomicInteger nbTry = new AtomicInteger(0);
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
if (nbTry.get() > 2) {
JOptionPane.showMessageDialog(null,
"Pin blocked due to 3 wrong tries");
return;
}
if (p1.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(p1);
for (String thePin : thePins) {
if (passEntered.equals(thePin)
&& p1.getText().length() == 4) {
System.out.println(":)");
authenticated = true;
break;
}
}
if (!authenticated) {
System.out.println(":(");
nbTry.incrementAndGet();
}
return null;
}
};
worker.execute();
}
}
// Accessing pins Bdd 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();
}
});
}
}
Alguma ideia ? Obrigado.
Florent
Aqui está o meu código editado com a solução de nachokk:
import java.io.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel container = new JPanel();
private JPasswordField p1 = new JPasswordField(4);
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();
PlainDocument document =(PlainDocument)p1.getDocument();
b.addActionListener(new BoutonListener());
top.add(label);
top.add(p1);
p1.setEchoChar('*');
top.add(b);
this.setContentPane(top);
this.setVisible(true);
document.setDocumentFilter(new DocumentFilter(){
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
String string =fb.getDocument().getText(0, fb.getDocument().getLength())+text;
if(string.length() <= 4)
super.replace(fb, offset, length, text, attrs); //To change body of generated methods, choose Tools | Templates.
}
});
container.add(p1);
}
class BoutonListener implements ActionListener {
private final AtomicInteger nbTry = new AtomicInteger(0);
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
if (nbTry.get() > 2) {
JOptionPane.showMessageDialog(null,
"Pin blocked due to 3 wrong tries");
return;
}
if (p1.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(p1);
for (String thePin : thePins) {
if (passEntered.equals(thePin)
&& p1.getText().length() == 4) {
System.out.println(":)");
authenticated = true;
break;
}
}
if (!authenticated) {
System.out.println(":(");
nbTry.incrementAndGet();
}
return null;
}
};
worker.execute();
}
}
// Fonction permettant d'accéder/lire notre BDD de pins (fichier .txt)
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();
}
});
}
}