Java 从另一个 class 调用时 PropertyChangeListener 不工作
Java PropertyChangeListener not working when called from another class
我有一个 class PanouAddContoriMetro
jtfSeria.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
// do something
}
}
从 PanouAddContoriMetro class : jtfSeria.setText("Test");
调用时工作
但当我将 属性 更改为另一个
时无法正常工作
class (PanouExpAutorizatii) : pacm.jtfSeria.setText("Test");
我认为这里的问题是您认为 PropertyChangeListener
会听 setText()
- 它不会。在 propertyChange
方法的文档中,它指出:
This method gets called when a bound property is changed.
此外,在 setText
方法的文档中:
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
.
因此,这是一个使用 DocumentListener
的示例。日志中显示为灰色的所有内容均由 PropertyChangeListener
触发,所有黑色内容均由 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();
}
});
}
}
我有一个 class PanouAddContoriMetro
jtfSeria.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
// do something
}
}
从 PanouAddContoriMetro class : jtfSeria.setText("Test");
但当我将 属性 更改为另一个
时无法正常工作class (PanouExpAutorizatii) : pacm.jtfSeria.setText("Test");
我认为这里的问题是您认为 PropertyChangeListener
会听 setText()
- 它不会。在 propertyChange
方法的文档中,它指出:
This method gets called when a bound property is changed.
此外,在 setText
方法的文档中:
Note that text is not a bound property, so no
PropertyChangeEvent
is fired when it changes. To listen for changes to the text, useDocumentListener
.
因此,这是一个使用 DocumentListener
的示例。日志中显示为灰色的所有内容均由 PropertyChangeListener
触发,所有黑色内容均由 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();
}
});
}
}