java documentlistener

Ser*_*pin 5 java swing jtextfield documentlistener

我正在尝试在更改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) 
            {

            }
        });
Run Code Online (Sandbox Code Playgroud)

当我在另一个ActionListener上调用此方法时,它可以正常工作.但是当我在文本字段中更改文本时,没有任何反应.甚至印刷品.有什么建议?

Ser*_*pin 9

问题解决了.仅当其他属性(字体,大小,而不是文本)发生更改时,才会调用changedUpdated方法.要在每次更改文本后调用方法,我应该将调用放入insertUpdate和removeUpdate方法.这条路:

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));
            }
        });
Run Code Online (Sandbox Code Playgroud)


mak*_*mov 1

尝试使用ActionListener

textField.addActionListener(this);

...
public void actionPerformed(ActionEvent evt) {
   String s = textField.getText();
   System.out.println(s);
   ...
}
Run Code Online (Sandbox Code Playgroud)