简体   繁体   中英

XPages - Using Java to copy text to local clipboard

I'm trying to use Java code in an XPages application to copy text to the local Windows clipboard. My code (executed by clicking a button) copies the code and outputs it to the IBM / Lotus Domino server console. I would like to be able to paste the copied text locally (on my Windows 7 PC) using Ctrl-V but this does not work. What change does my code need to make this work?

import java.awt.datatransfer.*;
import java.awt.Toolkit;
import java.io.*;

        Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();

        StringSelection testData;

        //  Add some test data 

        testData = new StringSelection( "New Test Data" );

        c.setContents(testData, null);

        //  Get clipboard contents, as a String

        Transferable t = c.getContents( null );

        if ( t.isDataFlavorSupported(DataFlavor.stringFlavor) )
        {
            Object o = t.getTransferData( DataFlavor.stringFlavor );
            String data = (String)t.getTransferData( DataFlavor.stringFlavor );
            System.out.println( "Clipboard contents: " + data );
        }

        System.exit(0);

This is not possible with Java. The Java code runs on server and has no possibilities to "fill" client's clipboard.

If you don't depend on IE or older browser versions then use HTML5 Clipboard API in CSJS. ( https://stackoverflow.com/a/26336421/2065611 http://datatables.net/blog/2014-01-31#Clipboard-API ). Get the text for clipboard from server with XSP.partialRefreshGet and execute ClipboardEvent 'copy' onComplete.

In addition to Knut Herrmanns answer: it is not possible to copy text to the user's clipboard using for instance client-side Javascript.

However, you can use Flash for this. I have used ZeroClipboard for this in a project.

Update: Knut updated his answer with a reference to the HTML5 Clipboard API. So my answer is only needed if you need to support IE and older browsers - and who isn't :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM