Reroute all document changes to a change listener for JTextArea - Java Swing

Java examples for Swing:JTextArea

Description

Reroute all document changes to a change listener for JTextArea

Demo Code

/***/*  w  ww  .  j  a va 2 s . co m*/
 * Copyright (C) 2010 Johan Henriksson
 * This code is under the Endrov / BSD license. See www.endrov.net
 * for the full text and how to cite.
 */
//package com.java2s;

import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class Main {
    /**
     * Reroute all document changes to a change listener
     */
    public static void textAreaChangeListener(JTextArea a,
            final ChangeListener list) {
        a.getDocument().addDocumentListener(new DocumentListener() {
            public void change() {
                list.stateChanged(null);
            }

            public void changedUpdate(DocumentEvent e) {
                change();
            }

            public void removeUpdate(DocumentEvent e) {
                change();
            }

            public void insertUpdate(DocumentEvent e) {
                change();
            }
        });
    }

    /**
     * Reroute all document changes to a change listener
     */
    public static void textAreaChangeListener(JTextField a,
            final ChangeListener list) {
        a.getDocument().addDocumentListener(new DocumentListener() {
            public void change() {
                list.stateChanged(null);
            }

            public void changedUpdate(DocumentEvent e) {
                change();
            }

            public void removeUpdate(DocumentEvent e) {
                change();
            }

            public void insertUpdate(DocumentEvent e) {
                change();
            }
        });
    }
}

Related Tutorials