Clipboard FlavorListener acting unreliable
I'm using the FlavorListener:flavorsChanged:FlavorEvent to detect changes on the clipboard...
...but sometimes Java does not throw an event when the clipboard changes.
Is this a known bug in the JVM (i'm using 1.5 & 1.6)? Or is it just in my code somewhere?
It seems to run fine when I test it in netbeans. When I build a jar, it doesn't work anymore after a while.
All tests have been done on Windows XP (& Vista).
Code Snippet:
privatevoid addClipListener(){
/* een luisteraar definieren */
ClipListener =new FlavorListener(){
publicvoid flavorsChanged(FlavorEvent e){
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
try{
Transferable content = clipboard.getContents(this);
if (content ==null){
System.out.println("Content is empty!");
return;
}
if (!content.isDataFlavorSupported(DataFlavor.stringFlavor)){
System.out.println("StringFlavor not supported!");
return;
}
try{
String data = (String)content.getTransferData(DataFlavor.stringFlavor);
clipboard.setContents(content,new ClipboardOwner(){
publicvoid lostOwnership(Clipboard clipboard, Transferable contents){
String datas ="";
try{
datas = (String)contents.getTransferData(DataFlavor.stringFlavor);
}catch (UnsupportedFlavorException ex){
ex.printStackTrace();
System.out.println("prob1");
}catch (IOException ex){
ex.printStackTrace();
System.out.println("prob2");
}
/* relevant data found */
System.out.println("DATA FOUND");
}
});
System.out.println("Content: \"" + data +"\"");
}catch (UnsupportedFlavorException ex){
ex.printStackTrace();
System.out.println("prob1");
}catch (IOException ex){
ex.printStackTrace();
System.out.println("prob2");
}
}catch (IllegalStateException ex){
ex.printStackTrace();
System.out.println("prob3");
}
}
};
clipboard.addFlavorListener(ClipListener);
}
privatevoid removeClipListener(){
clipboard.removeFlavorListener(ClipListener);
}
Any ideas what I'm doing wrong. Help appreciated :-)
null

