A question about DocumentListener

How do I use the changedUpdate(DocumentEvent e) method of DocumentListener. It should be invoked when the attributes changed. But why this method invoked under such situation see below. I add a button and a text pane in my program. When I click the button, the changedUpdate(DocumentEvent e) method will be invoked.

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.FileReader;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextPane;

import javax.swing.event.DocumentEvent;

import javax.swing.event.DocumentListener;

publicclass SwingFrameextends JFrame{

public SwingFrame(){

}

/**

* @param args

*/

publicstaticvoid main(String[] args){

// TODO Auto-generated method stub

JPanel panel =new JPanel();

panel.add(new JButton("button"));

final JTextPane textPane =new JTextPane();

File file =new File("/tmp/test.txt");

try{

FileReader in =new FileReader(file);

textPane.read(in,null);

in.close();

}catch (Exception e){

// TODO: handle exception

e.printStackTrace();

}

final MyListener listener =new MyListener();

textPane.getDocument().addDocumentListener(listener);

JButton button =new JButton("button");

button.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

// TODO Auto-generated method stub

File file =new File("/tmp/test.txt");

try{

FileReader in =new FileReader(file);

textPane.read(in,null);

in.close();

}catch (Exception ex){

// TODO: handle exception

ex.printStackTrace();

}

textPane.getDocument().addDocumentListener(listener);

}

});

JFrame frame =new JFrame();

frame.setLayout(new FlowLayout());

frame.add(textPane);

frame.add(button);

frame.setSize(200, 200);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

staticclass MyListenerimplements DocumentListener{

publicvoid changedUpdate(DocumentEvent e){

// TODO Auto-generated method stub

System.out.println("changed");

}

publicvoid insertUpdate(DocumentEvent e){

// TODO Auto-generated method stub

System.out.println("insert");

}

publicvoid removeUpdate(DocumentEvent e){

// TODO Auto-generated method stub

System.out.println("remove");

}

}

}

[5439 byte] By [youhaodiyia] at [2007-11-27 10:12:14]
# 1

For a start your adding the DocumentListener to the textpane twice.... once near the beginning of the method and then you add it again in the actionPerformed method

So each time you click the button your adding another listener...

And the changedUpdate method is doing exactly what it's supposed to... it's firing when the document changes... which it does each time you click the button

c0demonk3ya at 2007-7-28 15:19:01 > top of Java-index,Desktop,Core GUI APIs...
# 2

If i removed the addDocumentListener from the actionPerformed method, the DocumentListener installed before won't work again.

youhaodiyia at 2007-7-28 15:19:01 > top of Java-index,Desktop,Core GUI APIs...
# 3

The "changed" event gets fired when the new document is populated, even though you didn't apply any styles to the new text. You don't expect to see the event becasue you added the listener after the document was created, but that isn't good enough. To make sure the event is flushed out of the pipeline before the listener comes online, add the listener within an invokeLater() call: SwingUtilities.invokeLater(new Runnable() {

public void run() {

textPane.getDocument().addDocumentListener(listener);

}

});

You should do that in both places. Also, you should remove the listener from the old document before you load a new one, or the old documents will never get garbage collected.

uncle_alicea at 2007-7-28 15:19:01 > top of Java-index,Desktop,Core GUI APIs...