I am trying to implement copy-paste objects between different instances of the same application. Currently, it only works in one application (I mean copying and pasting in the same instance of the application), but it does not work between different instances.
Copy Code:
After that, I can check the contents of the clipboard, for example:
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable clipboardContent = clipboard.getContents(this); DataFlavor[] flavors = clipboardContent.getTransferDataFlavors(); System.out.println("flavors.length=" + flavors.length); for (int i = 0; i < flavors.length; i++){ System.out.println("flavor=" + flavors[i]); }
If I do this from the same application instance that I copied the object, it works: flavors.length is 1 , mimetype application/x-java-serialized-object , and, well, it works.
But if I open the application, execute the copy, then I open the same application again (the first one does not close, that is, both instances start at the same time) and try to check the contents of the clipboard from there, then flavors.length is 0 .
I reviewed the documents and these examples: one , two , but I still canβt find what is wrong in my implementation.
Did I miss something?
UPD: I added sscce: clipboard_test.zip .
This is a test application (I use Eclipse to create it) with three source files:
ClipboardTest.java - the main class of the applicationMyObject.java - a class for objects to copy / paste (this class contains only an array of String )MyObjectSelection.java - a class that implements the Transerable and ClipboardOwner interfaces
There are two buttons: "copy", "test".
When you click the copy button, a new instance of MyObject is created and placed on the clipboard.
When you press the test button, the contents of the clipboard are checked and the console echoes (the number of supported DataFlavor and each DataFlavor )
So, repeat these steps:
Launch the application
- Press the "copy" button: you will see the message
"object copied" in the log Press the "test" button: you will see the contents of the clipboard:
flavors.length = 1 flavor[0] = java.awt.datatransfer.DataFlavor[mimetype=application/x-java-serialized-object;representationclass=MyObject]
Launch another instance of the application (do not close the first)
Click the "test" button: you will see that the clipboard is empty:
flavors.length = 0
Why is this?
UPD2: sscce sent right here:
import java.awt.BorderLayout; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Toolkit; import javax.swing.JButton; import javax.swing.SwingUtilities; import javax.swing.JFrame; public final class ClipboardTest implements Runnable, ClipboardOwner { public static void main(String[] args) { SwingUtilities.invokeLater (new ClipboardTest()); } public void run() { JFrame f = new JFrame ("Clipboard test"); f.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);