Copy the image and text to the clipboard at the same time.

Source: Internet
Author: User
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;
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.