5

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Sergey Scopin
  • 2,217
  • 9
  • 39
  • 67
  • 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. – kentcdodds May 25 '12 at 15:52

4 Answers4

9

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));
            }
        });
Sergey Scopin
  • 2,217
  • 9
  • 39
  • 67
1

Try using an ActionListener:

textField.addActionListener(this);

...
public void actionPerformed(ActionEvent evt) {
   String s = textField.getText();
   System.out.println(s);
   ...
}
maksimov
  • 5,792
  • 1
  • 30
  • 38
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())
Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
  • 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. – GKFX Apr 11 '16 at 13:36
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));
        }
    });
Brian
  • 2,494
  • 1
  • 16
  • 21