简体   繁体   中英

Java PropertyChangeListener not working when called from another class

I have a class PanouAddContoriMetro with

jtfSeria.addPropertyChangeListener(new PropertyChangeListener() {

  public void propertyChange(PropertyChangeEvent evt) {
               // do something
        }
}

working when called from PanouAddContoriMetro class : jtfSeria.setText("Test");

but not working when I change the property from another

class (PanouExpAutorizatii) : pacm.jtfSeria.setText("Test");

I think the problem here is that you think a PropertyChangeListener will listen to setText() - It doesn't. In the documentation of the propertyChange method it states:

This method gets called when a bound property is changed.

Furthermore in the setText method's documentation:

Note that text is not a bound property, so no PropertyChangeEvent is fired when it changes. To listen for changes to the text, use DocumentListener .


So, here is an example using a DocumentListener . Everything appearing gray in the log was fired by the PropertyChangeListener , everything black by the DocumentListener :

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class Example {

    private JTextPane log;
    private int counter;

    public Example() {
        log = new JTextPane();
        log.setEditable(false);

        JTextField field = new JTextField(15);
        field.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                appendMessage(evt.getPropertyName(), Color.GRAY);
            }
        });
        field.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void changedUpdate(DocumentEvent e) {
                appendMessage("Changed", Color.BLACK);
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                appendMessage("Inserted", Color.BLACK);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                appendMessage("Removed", Color.BLACK);
            }
        });
        field.setText("Test");

        JFrame frame = new JFrame();
        frame.add(field, BorderLayout.NORTH);
        frame.add(new JScrollPane(log));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private void appendMessage(String message, Color color) {
        StyledDocument doc = log.getStyledDocument();
        Style style = log.addStyle("style1", null);
        StyleConstants.setForeground(style, color);
        try {
            doc.insertString(doc.getLength(), ++counter + ". " + message + "\n", style);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Example();
            }
        });
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM