Java JPasswordField makeJPasswordField(Document d, String s, int len, Object listener)

Here you can find the source of makeJPasswordField(Document d, String s, int len, Object listener)

Description

Factory Method to create Password Filed using specified params

License

Open Source License

Declaration

public static JPasswordField makeJPasswordField(Document d, String s, int len, Object listener) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.awt.Color;

import java.awt.SystemColor;
import java.awt.event.ActionListener;

import javax.swing.JPasswordField;

import javax.swing.event.DocumentListener;

import javax.swing.text.Document;

public class Main {
    public static final int DEFAULT_TEXTFIELD_WIDTH = 30;

    /**// ww w . ja  v  a 2  s.  co m
     * Factory Method to create Password Filed using specified params
     */
    public static JPasswordField makeJPasswordField(Document d, String s, int len, Object listener) {
        JPasswordField pf = new JPasswordField(DEFAULT_TEXTFIELD_WIDTH) {
            public void setEnabled(boolean enabled) {
                super.setEnabled(enabled);
                super.setEditable(enabled);
                setBackground(enabled ? Color.white : SystemColor.window);
                this.repaint();
            }
        };
        pf.setEnabled(true);
        if (d != null)
            pf.setDocument(d);
        if (s != null)
            pf.setText(s);
        if (len != -1)
            pf.setColumns(len);

        pf.addActionListener((ActionListener) listener);
        //detect text changes
        pf.getDocument().addDocumentListener((DocumentListener) listener);
        return pf;
    }
}

Related

  1. showPassword(JPasswordField pf, JCheckBox cb)