[英]JLabel on change text event
How I can retrive the event on a JLabel when change the text inside?? 如何在更改内部文本时在JLabel上检索事件?
I have a JLabel and when change the text inside I have to update other field. 我有一个JLabel,当更改内部文本时,我必须更新其他字段。
techically, the answer is to use a PropertyChangeListener and listen to changes of the "text" property, something like 从技术上讲,答案是使用PropertyChangeListener并监听“text”属性的更改,例如
PropertyChangeListener l = new PropertyChangeListener() {
public void propertyChanged(PropertyChangeEvent e) {
// do stuff here
}
};
label.addPropertyChangeListener("text", l);
not so technically: could be worth to re-visit the overall design and bind to original source which triggered the change in the label 技术上并非如此:可能值得重新访问整体设计并绑定到触发标签更改的原始来源
IMHO you can not get an event on JLabels textchange. 恕我直言,你不能在JLabels textchange上获得一个事件。 But you can use a JTextField instead of a JLabel: 但是您可以使用JTextField而不是JLabel:
private JTextField textFieldLabel = new JTextField();
textFieldLabel.setEditable(false);
textFieldLabel.setOpaque(true);
textFieldLabel.setBorder(null);
textFieldLabel.getDocument().addDocumentListener(new DocumentListener() {
public void removeUpdate(DocumentEvent e) {
System.out.println("removeUpdate");
}
public void insertUpdate(DocumentEvent e) {
System.out.println("insertUpdate");
}
public void changedUpdate(DocumentEvent e) {
System.out.println("changedUpdate");
}
});
Note: this event is fired no matter how the text gets changed; 注意:无论文本如何更改,都会触发此事件; programatically via "setText()" on the TextField or (if you do not "setEditable(false)") via clipboard cut/paste, or by the user typing directly into the field on the UI. 以编程方式通过TextField上的“setText()”或(如果你没有“setEditable(false)”)通过剪贴板剪切/粘贴,或者用户直接在UI上的字段中键入。
The lines: 线条:
textFieldLabel.setEditable(false);
textFieldLabel.setOpaque(true);
textFieldLabel.setBorder(null);
are used to make the JTextField look like an JLabel. 用于使JTextField看起来像JLabel。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.