Akhilesh Dhar Dubey
Akhilesh Dhar Dubey

Reputation: 2148

how to paste data from clipboard to textarea

When I use getTransferData(DataFlavor.stringFlavor) to paste some text from clipboard like:

Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
    String text=null;
    if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
         text= (String) t.getTransferData(DataFlavor.stringFlavor);
    }

I get exception javax.swing.text.BadLocationException: Invalid location. Why does this happen ?

Upvotes: 1

Views: 595

Answers (1)

Caffeinated
Caffeinated

Reputation: 12484

From the API:

attempts to reference a location that doesn't exist.

So you'll have to double-check in the GUI code.

But this appears to be an error within the Swing part, not because of getTransferData(DataFlavor.stringFlavor) itself

Upvotes: 1

Related Questions