首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在DocumentListener中访问JTextField

在DocumentListener中访问JTextField
EN

Stack Overflow用户
提问于 2013-03-21 06:04:58
回答 1查看 174关注 0票数 3

所以,我完成了一个数独解算器的制作,但我想要改进它。为此,我需要以某种方式从documentListener访问我的betterJTextField。我正在使用documentListener从我的betterJTextFields中实时读取数据,问题是在insertUpdate(DocumentEvent e)中。

我需要联系到发生DocumentEventbetterJTextfield。例如,如果输入无效,betterJTextfield将变为红色等。

如果你想知道,我把我所有的betterJTextfield都放在一个矩阵里。每个字段处理数独中的一个数字。

代码语言:javascript
复制
@Override
    public void insertUpdate(DocumentEvent e) {

       //Removed code which checks if the input in the betterJTextField is fine. 

    }

(JFormattedTextfield扩展了JTextField)

代码语言:javascript
复制
public class betterJTextField extends JFormattedTextField {
private int row;
private int column;

public betterJTextField(Format format, int row, int column) {
    super(format);
    this.row = row;
    this.column = column;
    // TODO Auto-generated constructor stub
}

public int getRow() {
    return row;
}

public int getColumn() {
    return column;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-21 06:17:04

我真的不完全理解你在问什么,但我相信这就是你想要的:

代码语言:javascript
复制
private static class RedDocumentListener implements DocumentListener {
    private JTextField textField;

    public RedDocumentListener(JTextField textField) {
        this.textField = textField;
    }
    @Override
    public void insertUpdate(DocumentEvent e) {
        textField.setBackground(Color.red);
    }
    @Override
    public void removeUpdate(DocumentEvent e) {
        textField.setBackground(Color.red);
    }
    @Override
    public void changedUpdate(DocumentEvent e) {
        textField.setBackground(Color.red);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15535679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档