How to display mask in jtextfield?

I want to display a mask in a textbox. How can i do this? Is there a reusable java library for this? I want to create a textbox that only allows eight digits to be entered (each digit must be 0 or 1)

eg.

enter image description here

+2


source to share


1 answer


This will create a hidden text box. When you hit the enter button, what the user entered is displayed. This is using JPasswordField. http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JPasswordField.html



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PasswordField1 extends JApplet implements ActionListener {
  /* Declaration */
  private JPasswordField Input;
  private JTextField Echo;
  private Container Panel;
  private LayoutManager Layout;

  public PasswordField1 () {
    /* Instantiation */
    Input = new JPasswordField ("", 20);
    Layout = new FlowLayout ();
    Panel = getContentPane ();

    /* Location */
    Panel.setLayout (Layout);
    Panel.add (Input);

    /* Configuration */
    Input.addActionListener (this);
  }

  public void actionPerformed (ActionEvent e) {
    char [] Chars;
    String Word;
    Chars = Input.getPassword();
    Word = new String(Chars);
    System.out.println("You Entered: " + Word);
  }
}

      

+2


source







All Articles