1

I am making a chat program that will paste the text the user puts in a certain number of times which essentially crowds the victims screen with a bunch of messages. I have already copied the text on the clipboard but I am not able to figure out how to paste it back without using the robot class which I don't want to do because I am not able to figure out how to press the command key for mac so it wouldn't be multi-platform and because if somebody presses a key in the middle it could do something completely different. If anybody can help me use the Clipboard class to paste that would be awesome. I've looked this up on like a billion different sites but can't figure it out. Thanks in advance! :)

Globmont
  • 11
  • 1
  • So are you writing a program to paste a message multiple times into an existing chat program or writing a chat program that sends a message multiple times? Your question doesn't make it very clear but I suspect the former... – Brian Beckett Jul 08 '11 at 14:23
  • 1
    I'm sorry I was vague. I am making a program that pastes a message multiple times into an existing chat program. Now of course this doesn't have to be used for chat. It can also be used if you have to write something like a billion times like if you have like 20 else if conditions. But it was the first one of your things. :) – Globmont Jul 08 '11 at 14:26

2 Answers2

1

http://www.javapractices.com/topic/TopicAction.do?Id=82

sshannin
  • 2,767
  • 1
  • 22
  • 25
  • Thanks for the reply but this tells me how to get whats on the clipboard in string form. I was wondering how would I actually paste it. Like simulating a CTRL+V without using the robot class. – Globmont Jul 08 '11 at 14:20
1

The following shows how to add text to the clipboard and how to get text from the clipboard.

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

class ClipboardTest
{
    public static void main(String[] args)
        throws UnsupportedFlavorException, IOException
    {
        Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
        StringSelection testData;

        //  Add some test data

        if (args.length > 0)
            testData = new StringSelection( args[0] );
        else
            testData = new StringSelection( "Test Data" );

        c.setContents(testData, testData);

        //  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);
    }
}

Once you have the text you can add it to a text component by doing:

Document doc = textComponent.getDocument();
doc.insertString(....);
camickr
  • 321,443
  • 19
  • 166
  • 288