English 中文(简体)
java 文档列表器
原标题:java 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) 
            {

            }
        });

当我在另一个“行动倾听者”上称这种方法为“行动倾听者”时,它可以发挥作用。但是,当我在文本字段中修改文本文本时,没有发生任何情况。甚至打印。有什么建议吗?

问题回答

问题解决了 。 更改了更新的方法, 仅在其它属性( 字体、 大小, 但不是文本) 更改时才被调用 。 如果要在每次文本修改后调用方法, 我应该将调用方式插入更新和删除更新方法 。 这样 :

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));
            }
        });

尝试使用 ActionListener 来尝试 ActionListener :

textField.addActionListener(this);

...
public void actionPerformed(ActionEvent evt) {
   String s = textField.getText();
   System.out.println(s);
   ...
}

我找到这个解决办法最快

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
              }             
            }
        }
    }

速度远比:

.getDocument().addDocumentListener( .... changeUpdate())

这是解决您问题的另一个办法。 您不必在每种方法下重复相同的代码, 而是可以创建一种方法, 并称之为更改更新、 插入更新、 删除更新的方法 。

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));
        }
    });




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签