Java JTextField Create makeJTextField(Document d, String s, int len, Object listener)

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

Description

Factory Method to create TextFiled using specified params

License

Open Source License

Declaration

public static JTextField makeJTextField(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.JTextField;

import javax.swing.event.DocumentListener;

import javax.swing.text.Document;

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

    /**/* ww w  .  j ava  2 s. co m*/
     * Factory Method to create TextFiled using specified params
     */
    public static JTextField makeJTextField(Document d, String s, int len, Object listener) {

        JTextField pf = new JTextField(DEFAULT_TEXTFIELD_WIDTH) {
            public void setEnabled(boolean enabled) {
                super.setEnabled(enabled);
                //super.setEditable(enabled);
                super.setBackground(enabled ? Color.white : SystemColor.window);
            }
        };
        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. createJTextField(Container c, String text, int x, int y, int width)
  2. createJTextField(Rectangle bounds)
  3. createJTextField(String name, int value, FocusListener fl)
  4. initJTextField(JTextField text, String element)
  5. initLabelComponent(final JLabel label, final JTextField textField)