【问题标题】:Reaching JTextField in a DocumentListener在 DocumentListener 中访问 JTextField
【发布时间】:2013-03-10 06:19:31
【问题描述】:

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

我需要到达DocumentEvent发生的betterJTextfield。例如,如果输入无效,betterJTextfield会变成红色等。

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

@Override
    public void insertUpdate(DocumentEvent e) {

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

    }

(JFormattedTextfield 扩展 JTextField)

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;
}

【问题讨论】:

    标签: java swing jtextfield sudoku documentlistener


    【解决方案1】:

    我不太明白你在问什么,但我相信这就是你要找的:

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-18
      • 2013-05-23
      • 2016-10-13
      • 2013-02-16
      • 2013-10-11
      • 2012-10-25
      • 2017-07-16
      • 1970-01-01
      • 2013-04-15
      相关资源
      最近更新 更多