Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java documentlistener

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?

like image 306
Sergey Scopin Avatar asked May 25 '12 15:05

Sergey Scopin


2 Answers

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));
            }
        });
like image 77
Sergey Scopin Avatar answered Oct 02 '22 19:10

Sergey Scopin


Try using an ActionListener:

textField.addActionListener(this);

...
public void actionPerformed(ActionEvent evt) {
   String s = textField.getText();
   System.out.println(s);
   ...
}
like image 31
maksimov Avatar answered Oct 02 '22 18:10

maksimov