java documentlistener
Asked Answered
G

4

6

I'm trying to call method after changing text of JTextField.

textField.getDocument().addDocumentListener(new DocumentListener()
        {

            public void changedUpdate(DocumentEvent arg0) 
            {
                System.out.println("IT WORKS");
                panel.setPrice(panel.countTotalPrice(TabPanel.this));
            }
            public void insertUpdate(DocumentEvent arg0) 
            {

            }

            public void removeUpdate(DocumentEvent arg0) 
            {

            }
        });

When I call this method at another ActionListener, it works ok. But when I change text in text field, nothing happens. Even println. Any suggestions?

Geanticline answered 25/5, 2012 at 15:50 Comment(1)
Are you trying to change the value in the textField? If so, you can't because document listeners can't change the value in the text field they're assigned to.Melloney
G
10

The problem solved. changedUpdated method called only when other atributes (font, size, but not text) changed. To call method after every change of text, I should put the call into insertUpdate and removeUpdate methods. This way:

textField.getDocument().addDocumentListener(new DocumentListener()
        {

            public void changedUpdate(DocumentEvent arg0) 
            {

            }
            public void insertUpdate(DocumentEvent arg0) 
            {
                System.out.println("IT WORKS");
                panel.setPrice(panel.countTotalPrice(TabPanel.this));
            }

            public void removeUpdate(DocumentEvent arg0) 
            {
                System.out.println("IT WORKS");
                panel.setPrice(panel.countTotalPrice(TabPanel.this));
            }
        });
Geanticline answered 25/5, 2012 at 16:17 Comment(0)
S
1

Try using an ActionListener:

textField.addActionListener(this);

...
public void actionPerformed(ActionEvent evt) {
   String s = textField.getText();
   System.out.println(s);
   ...
}
Spitball answered 25/5, 2012 at 16:8 Comment(0)
C
0

I found this solution the fastest:

new JTextPane().addActionListener(new Key());

class Key extends KeyAdapter{
private static final Object lock = new Object();
        private static int keydiff=0;
        public void keyReleased(KeyEvent e) {
            switch(e.getKeyCode())
            {
                //IGNORE FUNCTIONAL KEYS
                case 38 :
                case 39 :
                case 37 :
                case 40 :
                case 17 :
                case 157 :
                case 10 : break;
                default : keydiff++;
            }

            if(keydiff!=0)
            {
              synchronized(lock){
                  keydiff=0;
                  //EVENT FIRED HERE
              }             
            }
        }
    }

It's much faster than:

.getDocument().addDocumentListener( .... changeUpdate())
Cheshvan answered 23/6, 2012 at 19:58 Comment(1)
This is a bit dubious. What if you later make it possible to change the field contents without the keyboard? (eg. a "reset field" button or undo.) What about copy-and-paste? It's safer to use the proper method.Comedic
R
0

Here is another solution to your problem. Instead of having to repeat the same code under each method, you could create a method and call that method for changedUpdate, insertUpdate, removeUpdate.

textField.getDocument().addDocumentListener(new DocumentListener()
    {

        public void changedUpdate(DocumentEvent arg0) 
        {
            printMyLines();
        }
        public void insertUpdate(DocumentEvent arg0) 
        {
            printMyLines();
        }

        public void removeUpdate(DocumentEvent arg0) 
        {
            printMyLines();
        }

        private void printMyLines()
        {
            System.out.println("IT WORKS");
            panel.setPrice(panel.countTotalPrice(TabPanel.this));
        }
    });
Rollicking answered 18/4, 2014 at 22:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.