itr*_*tro 8 java swing jtextfield documentlistener
我在我的应用程序中有一个textField,当用户在JList中的一个项目中单击时,它将以编程方式启动(textField.setText()).以后的用户将手动更改此值.我不得不使用文档侦听器来检测此文本字段中的更改.当以编程方式发生更改时,它必须执行任何操作,但如果手动发生,则应将背景更改为红色.
如何检测textField是手动填写还是通过textField.setText()填写?
txtMode.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
if (!mode.equals(e.getDocument()))
txtMode.setBackground(Color.red);
}
public void removeUpdate(DocumentEvent e) {
if (mode.equals(e.getDocument()))
txtMode.setBackground(Color.white);
}
public void changedUpdate(DocumentEvent e) {
//To change body of implemented methods
}
});
Run Code Online (Sandbox Code Playgroud)
有两种方法
DocumentListener在setText("...")添加之前删除DocumentListener码
public void attachDocumentListener(JComponent compo){
compo.addDocumentListener(someDocumentListener);
}
//similair void for remove....
Run Code Online (Sandbox Code Playgroud)
boolean值来禁用"如果需要",但你必须改变你的竞争对手DocumentListener 例如
txtMode.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
if (!mode.equals(e.getDocument()))
if (!updateFromModel){
txtMode.setBackground(Color.red);
}
}
public void removeUpdate(DocumentEvent e) {
if (mode.equals(e.getDocument()))
if (!updateFromModel){
txtMode.setBackground(Color.white);
}
}
public void changedUpdate(DocumentEvent e) {
//To change body of implemented methods
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3596 次 |
| 最近记录: |