0

我正在尝试在多个 JTextFields 上使用 DocumentListener。我需要知道哪个 textField 将进入 DocumentEvent,以便我可以执行一些特定的过程。下面是我如何进行DocumentListener编码和设置其中一个 JTextField [感谢此示例:如何获取放置 Document 的 JTextField 名称?](我的 textField 声明在更高的范围内)。

我该如何解决?

    final DocumentListener documentListener = new DocumentListener() {
        @Override
        public void changedUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
          }
          @Override
        public void insertUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
          }
          @Override
        public void removeUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
          }
          private void printIt(DocumentEvent documentEvent) {
            final DocumentEvent.EventType type = documentEvent.getType();
            String typeString = null;
            final JTextField textField = (JTextField) documentEvent.getDocument().getProperty("parent");
            if (type.equals(DocumentEvent.EventType.CHANGE)) {
              typeString = "(parent: " + textField + ") Change";
            }  else if (type.equals(DocumentEvent.EventType.INSERT)) {
              typeString = "(parent: " + textField + ") Insert";
            }  else if (type.equals(DocumentEvent.EventType.REMOVE)) {
              typeString = "(parent: " + textField + ") Remove";
            }
            System.out.print("Type : " + typeString);
            final Document source = documentEvent.getDocument();
            final int length = source.getLength();
            System.out.println("Length: " + length);
          }
        };

我的 JTextField 编码如下...

    textFieldFencing_LC1 = new JTextField();
    textFieldFencing_LC1.setHorizontalAlignment(SwingConstants.CENTER);
    textFieldFencing_LC1.setFont(new Font("Tahoma", Font.PLAIN, 9));
    textFieldFencing_LC1.setColumns(10);
    textFieldFencing_LC1.setBounds(234, 535, 85, 14);
    panelLC.add(textFieldFencing_LC1);
    textFieldFencing_LC1.getDocument().addDocumentListener(documentListener);
    textFieldFencing_LC1.getDocument().putProperty("parent",textFieldFencing_LC1);

我想要的输出应该是这样的

Type : (parent: textFieldFencing_LC1) InsertLength: 1
Type : (parent: textFieldFencing_LC1) InsertLength: 1

我得到的输出看起来像这样......

Type : (parent: javax.swing.JTextField[,234,535,85x14,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@384cdfdd,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=10,columnWidth=0,command=,horizontalAlignment=CENTER]) InsertLength: 2
Type : (parent: javax.swing.JTextField[,234,535,85x14,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@384cdfdd,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=10,columnWidth=0,command=,horizontalAlignment=CENTER]) InsertLength: 3
4

1 回答 1

0

在阅读了这个类的文档后,我意识到这个putProperty(Object,Object)方法可以让我把一个String放进去。所以,现在我的 JTextField 上的听众看起来像这样......

textFieldFencing_LC1.getDocument().addDocumentListener(documentListener);
textFieldFencing_LC1.getDocument().putProperty("parent","LC1");  // String, String

注意,putProperty() 中的第二个参数是一个对我有意义的字符串,所以我可以检查 LC#。虽然 DocumentListener 中的更新看起来像这样......

final DocumentEvent.EventType type = documentEvent.getType();
String typeString = null;

// Cast documentEvent to String
txtField.setText( (String) documentEvent.getDocument().getProperty("parent") );  

          if (type.equals(DocumentEvent.EventType.CHANGE)) {
                 typeString = "(parent: " + txtField.getText() + ") Change";
          }  else if (type.equals(DocumentEvent.EventType.INSERT)) {
                 typeString = "(parent: " + txtField.getText() + ") Insert";
          }  else if (type.equals(DocumentEvent.EventType.REMOVE)) {
                 typeString = "(parent: " + txtField.getText() + ") Remove";
       }
   System.out.print("Type : " + typeString);
   final Document source = documentEvent.getDocument();
   final int length = source.getLength();
   System.out.println("Length: " + length);
}

输出现在看起来像......

Type : (parent: LC1) RemoveLength: 0
Type : (parent: LC1) InsertLength: 1
Type : (parent: LC1) InsertLength: 2
Type : (parent: LC1) InsertLength: 3

底线:一个引用的类和两行代码放在我需要收听的 JTextFields 上,以便发生自动更新,这比在所有这些字段上添加 CaretListener 要好得多。

于 2016-01-20T02:31:55.980 回答