Import java. awt .*;
Import javax. swing .*;
Import java. awt. event .*;
Import javax. swing. event .*;
Import java. awt. dnd .*;
Import java. awt. datatransfer .*;
Import java. io .*;
Class ClipboardTest2
{
JFrame mainFrame;
JPanel mainPanel;
JButton button;
Clipboard cb;
Public ClipboardTest2 (){
MainFrame = new JFrame ();
MainPanel = new JPanel ();
Button = new JButton ("Button ");
Button. setIcon (new ImageIcon ("candle.png "));
Cb = Toolkit. getdefatooltoolkit (). getSystemClipboard ();
Button. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent e ){
ButtonTextAndImageTransferable btait =
New ButtonTextAndImageTransferable (button );
Cb. setContents (btait, btait );
}
});
MainPanel. add (button );
MainFrame. getContentPane (). add (mainPanel );
MainFrame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
MainFrame. pack ();
MainFrame. setLocationRelativeTo (null );
MainFrame. setVisible (true );
}
Public static void main (string [] ARGs)
{
Swingutilities. invokelater (New runnable (){
Public void run (){
New clipboardtest2 ();
}
});
}
}
Class buttontextandimagetransferable extends imageicon implements transferable, clipboardowner {
Dataflavor [] flavors;
Jbutton button;
Public void lostownership (Clipboard clipboard, transferable contents ){
System. Out. println ("lostownership ");
}
Public buttontextandimagetransferable (jbutton button ){
Flavors = new dataflavor [2];
Flavors [0] = dataflavor. stringflavor;
Flavors [1] = dataflavor. imageflavor;
This. Button = button;
}
Public DataFlavor [] getTransferDataFlavors (){
Return flavors;
}
Public Object getTransferData (DataFlavor flavor ){
If (flavor. equals (flavors [0])
{
Return button. getText ();
} Else {
If (flavor. equals (flavors [1]) {
Imageicon icon = (imageicon) button. getIcon ();
Return icon. getimage ();
}
}
Return NULL;
}
Public Boolean isdataflavorsupported (dataflavor flavor ){
If (flavor. Equals (flavors [0]) |
Flavor. Equals (flavors [1])
Return true;
Return false;
}
}