I created a JTextField and set the setDocument property with an extended class from PlainDocument.

When I'm erasing from the textbox I want to print the string that is remaining, but it prints just one step before.

let's say. If the JTextField has a string like 123456789 and I erase one char with the backspace then it prints ok (12345678). But if I delete let's say "3" then it shows "1234567". So Im printing a previous value. If I then delete another char, then it will show that 3 was erased. . only that it knows that the length changed and prints one less char at the back.

If I didnt make myself clear. I want to print the "real" string that the textfield has when deleting from the plainDocument code.

When I delete it triggers the remove, or removeupdate method there is when my text gets printed. (probably im printing one step earlier)

PS: I have to get the value from the plaindocument and not from the jtextfield in the outside.

this is my code

protected void removeUpdate(AbstractDocument.DefaultDocumentEvent evento)
 {                      
        try {               
            super.removeUpdate(evento);
            removePostUpdate(evento);

        } catch (BadLocationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 }

protected void removePostUpdate(AbstractDocument.DefaultDocumentEvent evento) throws BadLocationException
 {       
     try {              
            palabraCompleta = evento.getDocument().getDefaultRootElement().getDocument().getText(0,evento.getDocument().getDefaultRootElement().getDocument().getLength()-1 );                              
            System.out.println(evento.getPresentationName());               
        } catch (BadLocationException e) {          
            e.printStackTrace();
        }
        System.out.println(palabraCompleta);
 }
有帮助吗?

解决方案

I have replicated what you have done with a JTextField using its default document.

First of all, you code has a bug - you are using length - 1 instead of length. Also not sure you need Document.getDefaultRootElement().getDocument() that just seems wrong to me.

Anyway, the test code I created below runs without a problem. Given the bugs I found in the code you posted, I would say that IF there is actually a problem its with something you've customized that you haven't posted here


    public static void testDocument () {
        final JTextField tf = new JTextField();
        tf.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent arg0) {
                System.out.println(tf.getText());
                try {
                    System.out.println("printing: " + arg0.getDocument().getDefaultRootElement().getDocument().getText(0,arg0.getDocument().getDefaultRootElement().getDocument().getLength()));
                } catch (BadLocationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            @Override
            public void insertUpdate(DocumentEvent arg0) {
                System.out.println(tf.getText());               
                try {
                    System.out.println("printing: " + arg0.getDocument().getDefaultRootElement().getDocument().getText(0,arg0.getDocument().getDefaultRootElement().getDocument().getLength()));
                } catch (BadLocationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            @Override
            public void removeUpdate(DocumentEvent arg0) {
                System.out.println(tf.getText());               
                try {
                    System.out.println("doc: " + arg0.getDocument().getDefaultRootElement().getDocument().getText(0,arg0.getDocument().getDefaultRootElement().getDocument().getLength()));
                } catch (BadLocationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        });
        WindowUtilities.visualize(tf);
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top