0

I am trying to use a DocumentListener on a number of JTextFields. I need to know which textField is coming into the DocumentEvent so I can do some specific processes. Below is how I have my DocumentListener coded and one of the JTextFields set up [thanks to this example: How to get JTextField name in which is Document placed? ](my textField is declared at a higher scope).

How do I fix this?

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

My JTextField is coded like the following...

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

The output I want should look like this

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

The output I am getting looks like this ...

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
Community
  • 1
  • 1
EricG
  • 65
  • 9
  • Thought by putting `.getName()` on the textField, like so `textFieldFencing_LC1.getDocument().putProperty("parent",textFieldFencing_LC1.getName());` might solve the issue. But, it did not. Now the output reads as `Type : (parent: null) InsertLength: 1` – EricG Jan 19 '16 at 22:25

1 Answers1

0

After reading the documentation on this Class, I realized the putProperty(Object,Object) method would allow me to put a String into it. So, now my listener on my JTextField looks like this...

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

Note, the second parameter in the putProperty() is a String with meaning for me, so I can check for LC#. While the update in the DocumentListener looks like this...

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

Output now looks like...

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

Bottom line: a referenced Class and two lines of code placed on the JTextFields I need to listen to so an automatic update occurs, is a lot better than adding a CaretListener on all of those fields.

EricG
  • 65
  • 9