Java Clipboard Event Listener not working
-
18-10-2022 - |
Question
I am trying to implement clipboard change event listener and I really stuck. Can't find an working example of how to implement it or whatever I find is not complete or not working. So I am trying to implement a prototype of this functionality but it has a bug... So each time when the clipboard content changed I want to see a pop up menu
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.FlavorListener;
import java.awt.datatransfer.Transferable;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class MyTextArea extends JFrame implements ClipboardOwner, FlavorListener{
JTextArea textArea;
Clipboard clip;
public MyTextArea()
{
this.setSize(500, 500);
textArea = new JTextArea(40, 40);
clip = Toolkit.getDefaultToolkit().getSystemClipboard();
clip.setContents(clip.getContents(null), this);
clip.addFlavorListener(this);
Container cp = this.getContentPane();
cp.setLayout(new FlowLayout());
cp.add(textArea);
}
@Override
public void flavorsChanged(FlavorEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog (null, "Flavour Changed","Error", JOptionPane.ERROR_MESSAGE);
clip.removeFlavorListener(this);
clip.setContents(clip.getContents(null), this);
clip.addFlavorListener(this);
}
@Override
public void lostOwnership(Clipboard arg0, Transferable arg1) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
JFrame frame = new MyTextArea();
frame.setVisible (true);
frame.setDefaultCloseOperation (EXIT_ON_CLOSE);
}
}
Does anyone how to make this code working properly?
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow