Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) throws Exception {
- // The clipboard
- final Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
- // read clipboard and take ownership to get the FlavorListener notified
- // when the content has changed but the owner has not
- processClipboard(cb);
- cb.addFlavorListener(new FlavorListener() {
- @Override
- public void flavorsChanged(FlavorEvent e) {
- processClipboard(cb);
- }
- });
- // keep thread for testing
- Thread.sleep(100000L);
- }
- public static void processClipboard(Clipboard cb) {
- // gets the content of clipboard
- Transferable trans = cb.getContents(null);
- if (trans.isDataFlavorSupported(DataFlavor.stringFlavor)) {
- try {
- // cast to string
- String s = (String) trans
- .getTransferData(DataFlavor.stringFlavor);
- System.out.println(s);
- // only StringSelection can take ownership, i think
- StringSelection ss = new StringSelection(s);
- // set content, take ownership
- cb.setContents(ss, ss);
- } catch (UnsupportedFlavorException e2) {
- e2.printStackTrace();
- } catch (IOException e2) {
- e2.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement