Java JTextField bindPropertyChangeListener(final JTextField field, final String name, final PropertyChangeListener listener)

Here you can find the source of bindPropertyChangeListener(final JTextField field, final String name, final PropertyChangeListener listener)

Description

bind Property Change Listener

License

LGPL

Declaration

public static void bindPropertyChangeListener(final JTextField field,
            final String name, final PropertyChangeListener listener) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;
import javax.swing.text.StringContent;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class Main {
    public static void bindPropertyChangeListener(final JTextField field,
            final String name, final PropertyChangeListener listener) {
        bindPropertyChangeListener(field, name, listener, null);
    }//from   www .ja v a 2  s.co  m

    public static void bindPropertyChangeListener(final JTextField field,
            final String name, final PropertyChangeListener listener,
            DocumentFilter documentFilter) {

        if (field.getDocument() == null) {
            field.setDocument(new PlainDocument(new StringContent()));
        }

        final PlainDocument document = (PlainDocument) field.getDocument();

        if (documentFilter != null) {
            document.setDocumentFilter(documentFilter);
        }

        field.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                fireChangeEvent();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                fireChangeEvent();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                fireChangeEvent();
            }

            private void fireChangeEvent() {
                listener.propertyChange(new PropertyChangeEvent(field,
                        name, "", field.getText()));
            }
        });
    }
}

Related

  1. addStyle(JTextField textField, String labelName)
  2. adjustTextToRight(JTextField textField)
  3. applyDefaultProperties(final JTextField comp)
  4. AreAllTextFieldsFilled(javax.swing.JTextField[] tfs)
  5. badField(JTextField field)
  6. browseFileForField(JTextField textField, JFileChooser fileChooser, Component parent)
  7. checkJTextFieldNotEmpty(javax.swing.JTextField field, javax.swing.JLabel promptName, ArrayList errors)
  8. cleanTextFields(JTextField... textFields)
  9. createBrowseButton(final JTextField txtInput, final Window parent, final boolean directoryBrowser)