• 0

(JAVA) Need info from OS clipboard


Question

I am writing a plug-in for eclipise, for this i need to know where text copied in comes from

I know the datatransfer class has some methods but only to return name of clipboard, ie system.

how can i find more details on source (using java methods or os interface directly)

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

So you want to know how things get copied and pasted from the Clipboard?

public class ClipboardTest extends Frame 
                           implements ClipboardOwner, ActionListener {

    TextArea srcText, dstText;
    Button copyButton, pasteButton;

    Clipboard clipboard = getToolkit().getSystemClipboard();

    public ClipboardTest() {
        super("Clipboard Test");
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        setLayout(gridbag);

        srcText = new TextArea(8, 32);
        c.gridwidth = 2;
        c.anchor = GridBagConstraints.CENTER;
        gridbag.setConstraints(srcText, c);
        add(srcText);

        copyButton = new Button("Copy Above");
        copyButton.setActionCommand("copy");
        copyButton.addActionListener(this);
        c.gridy = 1;
        c.gridwidth = 1;
        gridbag.setConstraints(copyButton, c);
        add(copyButton);

        pasteButton = new Button("Paste Below");
        pasteButton.setActionCommand("paste");
        pasteButton.addActionListener(this);
        pasteButton.setEnabled(false);
        c.gridx = 1;
        gridbag.setConstraints(pasteButton, c);
        add(pasteButton);

        dstText = new TextArea(8, 32);
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 2;
        gridbag.setConstraints(dstText, c);
        add(dstText); 

        pack();
    }

    public void actionPerformed(ActionEvent evt) {
        String cmd = evt.getActionCommand();

        if (cmd.equals("copy")) { 
           // Implement Copy operation
           String srcData = srcText.getText();
           if (srcData != null) {
                StringSelection contents = new StringSelection(srcData);
                clipboard.setContents(contents, this);
                pasteButton.setEnabled(true);
            }
        } else if (cmd.equals("paste")) {
            // Implement Paste operation
            Transferable content = clipboard.getContents(this);
            if (content != null) {
                try {
                    String dstData = (String)content.getTransferData(
                                                DataFlavor.stringFlavor);
                    dstText.append(dstData);
                } catch (Exception e) {
                    System.out.println("Couldn't get contents in format: "+
                           DataFlavor.stringFlavor.getHumanPresentableName()); 
                }
             }
        }
    }
    public void lostOwnership(Clipboard clipboard, Transferable contents) {
       System.out.println("Clipboard contents replaced");
    }
     public static void main(String[] args) {
        ClipboardTest test = new ClipboardTest();
        test.show();
     }
}

Look specifically at the actionPerformed() method.

From: http://java.sun.com/j2se/1.3/docs/guide/aw...tatransfer.html

Link to comment
Share on other sites

  • 0

lets say user copied text (code) from notepad, from myfile.txt in c:\temp into my program window

i need to know how to figure out from the system clipboard that the location this is from is c:\temp\myfile.txt

Link to comment
Share on other sites

  • 0
lets say user copied text (code) from notepad, from myfile.txt in c:\temp into my program window

i need to know how to figure out from the system clipboard that the location this is from is c:\temp\myfile.txt

I'm not sure you could do that, at least easily. I mean you could see when someone hits ctrl + c, but only notepad really knows what file it opened. You can get the filename from its titlebar, but to get the location will be harder. I'm not sure there is one easy way to do what you want.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.