Using the Default Editor Kit to put text on system clipboard - java

I have been trying for a month now to reliably set the system clipboard in my program. Currently it works about 95/100 times. But I keep searching.
Yesterday I came upon this the Java DefaultEditorKit.copyAction and died a little inside seeing there was already something written that might do what I want.
Though the problem is that this is a "Action" for a dialog?
How can I issue the text I want copied to the clipboard? I do not wish to attach this "Action" to any button/component in my app. I want to be able to do
DefaultEditorKit.copyAction("Put this on Clipboard");
But this is undefined. I am not sure how to trigger this "action" and give it some text to work with?
EDIT: Here is my code that causes an exception.
public void setClip2(String arg)
{
while(true)
{
try
{
sysClip.setContents(new StringSelection(arg), null);
}
catch(Exception e)
{
try {Thread.sleep(20);} catch (InterruptedException e1) {}
continue;
}
break;
}
return;
}

I just wondered if there was a way to reliably set the clipboard. (this method fails if you do not wait long enough trying to set it, which is usually about 1-2 seconds
Not sure why you have to wait to set the contents of the clipboard.
This program doesn't have any problem refreshing the clipboard every 200ms. That is the number increments by 1 every time is it displayed as expected:
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;
class ClipboardLoopTest
{
public static void main(String[] args)
throws InterruptedException
{
for (int i = 0; i < 100; i++)
{
// add data to clipboard
try
{
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection testData;
testData = new StringSelection( "Test: " + i );
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 );
}
}
catch(Exception e)
{
System.out.println(e);
}
Thread.sleep(200);
}
System.exit(0);
}
}
I'm using JDK 7 on Windows 7.
Maybe you can post your SSCCE that demonstrates the problem.

DefaultEditorKit.copyAction actually use (through some layers) the functionnality in java.awt.datatransfer. There you will find classes to send data to the clipboard.
Basically, if you just want to send a string to the clipboard without using any Swing component, you setup a ClipboardOwner, you create a StringSelection object and you give it to the system clipboard. Here is a most basic example:
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents( new StringSelection("Put this on Clipboard"), new ClipboardOwner() {
#Override
public void lostOwnership(Clipboard clipboard, Transferable contents) {
System.out.println("I am no longer the clipboard owner.");
}
} );

Related

Java clipboad mistake

There is such a program. It must analyze the clipboard for the presence of a five-digit number in it. But when you first copy the text that meets the condition, the program works fine, but if you copy the second text in the same window, the program that meets the condition does not work. That is, it works only if you periodically change windows.
The question is to get the program to work with each copy?
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.IOException;
public class Main implements FlavorListener {
private static Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
public static void main(String[] args) throws InterruptedException {
clipboard.addFlavorListener(new Main());
// fall asleep for 100 seconds, otherwise the program will immediately end
Thread.sleep(100 * 1000);
}
#Override
public void flavorsChanged(FlavorEvent event) {
try {
String clipboardContent = (String) clipboard.getData(DataFlavor.stringFlavor);
handleClipboardContent(clipboardContent);
} catch (UnsupportedFlavorException | IOException e) {
// TODO handle error
e.printStackTrace();
}
}
private void handleClipboardContent(String clipboardContent) {
// we check that the length of the string is five
if (clipboardContent != null && clipboardContent.length() == 5)
{
System.out.println(clipboardContent);
}
else {
System.out.println("condition false");
}
}
}
// 12345
// 56789
The FlavorListener will notify you when the "type" of data in the Clipboard has changed, not when the data itself has changed. This means if you copy a String to the Clipboard, you "might" be notified, but if you copy another String to the Clipboard, you won't, because the type of data has not changed.
The "common" solution to the problem you're facing is to reset the contents of the clipboard to a different flavour. The problem with this is, what happens if some other program wants the data? You've just trampled all over it
Instead, you could "peek" at the data on a periodical bases and check to see if the contents has changed or not. A basic solution would be to use a Thread which maintained the hashCode of the current String contents, when the hashCode changes, you would then grab a copy and perform what ever operations you wanted on it.
Maybe something like...
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.FlavorListener;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.addFlavorListener(new FlavorListener() {
#Override
public void flavorsChanged(FlavorEvent e) {
System.out.println("Flavor has changed");
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
try {
String text = (String) clipboard.getData(DataFlavor.stringFlavor);
textDidChangeTo(text);
} catch (UnsupportedFlavorException | IOException ex) {
ex.printStackTrace();
}
}
}
});
Thread t = new Thread(new Runnable() {
private Integer currentHashcode;
#Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(this);
if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
try {
String text = (String) clipboard.getData(DataFlavor.stringFlavor);
if (currentHashcode == null) {
currentHashcode = text.hashCode();
} else if (currentHashcode != text.hashCode()) {
currentHashcode = text.hashCode();
textDidChangeTo(text);
}
} catch (UnsupportedFlavorException | IOException ex) {
ex.printStackTrace();
}
} else {
currentHashcode = null;
}
}
}
});
t.start();
}
public static void textDidChangeTo(String text) {
System.out.println("Text did change to: " + text);
}
}
Now, this is far from perfect. It may generate two events when the contents changes from something other then String to String. In this, based on your needs, you probably don't need the FlavorListener, but I've used it for demonstration purposes

How to determine current clipboard DataFlavor before get clip content

I have following trouble, when i copy Intellij Idea Editor text, and run
Toolkit.defaultToolkit.systemClipboard.getData(DataFlavor.stringFlavor)
it will raise:
Exception "java.lang.ClassNotFoundException: com/intellij/codeInsight/editorActions/FoldingData"while constructing DataFlavor for: application/x-java-jvm-local-objectref; class=com.intellij.codeInsight.editorActions.FoldingData
In fact, I hope ignore clip content with FoldingData, how to detect current clipboard DataFlavor
Clipboard data may be available in multiple flavors. Therefore, you should use Clipboard.getAvailableDataFlavors() and iterate through the array to determine if the DataFlavor you are looking for is there.
See http://docs.oracle.com/javase/7/docs/api/java/awt/datatransfer/Clipboard.html#getAvailableDataFlavors()
But if you are getting a ClassNotFoundException, this means that your runtime classpath is missing dependencies, so you need to fix this
This way you can check the DataFlavor earlier to avoid the UnsupportedFlavorException later
public class ClipBoard {
public static void main(String args[]) {
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try {
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String text = (String)t.getTransferData(DataFlavor.stringFlavor);
text=text.toUpperCase();
StringSelection ss = new StringSelection(text);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Read more: http://mrbool.com/manipulating-clipboard-content-with-java/24758#ixzz4tta4bwNQ

How to Save, Save As Java?

Hello I'm having a little bit of a problem with my text editor like program. I would like to have my Save feature work only if Save As has been called, and if Save is called that it appends the text From a JTextArea to the file created by Save As. I am using ActionListeners from JMenuItems to call the Save and Save As Actions. Here's the code for Save As:
FileDialog fileDialogSave = new FileDialog(frame, "Save", FileDialog.SAVE);
fileDialogSave.setVisible(true);
String userProjectSavePath = fileDialogSave.getDirectory() + fileDialogSave.getFile();
File userProjectSave = new File(userProjectSavePath);
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(userProjectSave, true)))) {
userProjectSave.createNewFile();
String userProjectNameToSave = codeArea.getText();
out.println(userProjectNameToSave);
} catch (IOException e1) {
e1.printStackTrace();
}
Both the Save and Save As are nested inside static class ActionSaveAs implements ActionListener { public void actionPerformed(ActionEvent e) { ... } }
The problem is I can't access the String userProjectSavePath in the Save class so I can't append the new text to the same file as in the Save As.
Instead, let your notional saveDocument() method invoke saveDocumentAs() if currentFile is null. The following outline suggests the approach taken in Charles Bell's HTMLDocumentEditor, cited here.
public void saveDocument() {
if (currentFile != null) {
// Save in currentFile
} else {
saveDocumentAs();
}
}
public void saveDocumentAs() {
// Check before overwriting existing file
}

My custom "paste from clipboard" action

I want to find a way to do kind of a custom "paste from clipboard" action. Let's assume the content of clipboard is text for simplicity (not a file). Whenever you press Ctrl+V, it inserts that content (which is text) to a current open file which has a focus.
I have an app for catching a global hotkey. Note this is not a window application, it's a console one and it catches the hotkey globally. Let's say I have the hotkey of Ctrl+U. So what I want to do is when I press Ctrl+U I want to insert some predefined text to a current open file. Just like Ctrl+V does! The differences from a standard Ctrl+V is that I want to insert a predefined text and the hotkey is different.
How do I do this?
I'd prefer a cross-platform solution, however first of all I'm going to do that for Linux, specifically Ubuntu. The language is not important but Java or Scala would be better. Of course, I understand that the solutions is Java uses native OS' API for that.
I'm hoping that this hackish solution would work, but it is still untested, I am unsure how to catch the event for the hotkey.
The idea behind this code is the following five steps:
Get the old text in the clipboard and temporarily save it
Paste our predefined text into the clipboard
Trigger the global paste event
Release the global paste event
Reset the clipboard to the old text
This should give the appearance of a new clipboard (if not, hopefully it inspires you to come up with a better, less hackish solution).
Without further ado, here is my code. First I have a simple helper method to set the value of the clipboard (as we do this twice).
public static void setClipboard(String s) {
StringSelection contents = new StringSelection(s);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(contents, contents);
}
And then, I have a main method where I go through the five steps in order.
public static void main(String[] args) {
// Step 1 ) get old text
String oldText = "";
try {
oldText = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
// Step 2 ) paste our text in clipboard
setClipboard("This lorem ipsum predefined string blows my mind.");
// Step 3 ) trigger paste event
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException awte) {
awte.printStackTrace();
}
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
// Step 4 ) Release paste event
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
// Step 5 ) Reset clipboard
setClipboard(oldText);
}
[Edit]:
Here is some code to test what kind of contents are in the Clipboard - image, text, etc. The unicode error was coming from the fact that the old contents of the clipboard were something that couldn't be represented by a plain String. To fix this error, you will have to check if the old contents were an image, the old contents were text, and save them accordingly.
public static int kindOfContents() {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
if(contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
// String, save temporarily as string and write back as string
return 0;
} else if(contents.isDataFlavorSupported(DataFlavor.imageFlavor)) {
// Image, save temporarily as BufferedImage and write back as image
return 1;
} else if(contents.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
// List of files, save temporarily as java.util.List interface and write back as the file lists
return 2;
}
}
If the contents are text, then for saving and writing the content you would use the old method, repasted below for convenience.
// Step 1 ) get old text
String oldText = "";
try {
oldText = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
// Step 5 ) Reset clipboard
setClipboard(oldText);
However, if the contents are an image, then for saving temporarily and rewriting you need to do the following. Note that the code for writing the image is not mine, but is taken from the accepted answer at Setting images to Clipboard - Java
// Step 1 ) get old image
BufferedImage img = null;
try {
img = (BufferedImage) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.imageFlavor);
} catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
Taken from Setting images to Clipboard - Java :
// Step 5 ) Reset clipboard
ImageTransferable transferable = new ImageTransferable( image );
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferable, null);
static class ImageTransferable implements Transferable
{
private Image image;
public ImageTransferable (Image image)
{
this.image = image;
}
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException
{
if (isDataFlavorSupported(flavor))
{
return image;
}
else
{
throw new UnsupportedFlavorException(flavor);
}
}
public boolean isDataFlavorSupported (DataFlavor flavor)
{
return flavor == DataFlavor.imageFlavor;
}
public DataFlavor[] getTransferDataFlavors ()
{
return new DataFlavor[] { DataFlavor.imageFlavor };
}
}

How to paste from system clipboard content to an arbitrary window using Java

I would like to write a Java program that has a button. When the button is pressed it pastes / drops the content of the system clipboard to the text field that currently has focus within an arbitrary, possibly non-Java app (say MS Word). Essentially the button action has to simulate the sending of CTRL-V (paste) action somehow.
Does any one have any suggestions?
The package java.awt.datatransfer seems to be a solution, according to this article. Here is another article.
From the latter page, the needed imports:
import java.awt.datatransfer.*;
import java.awt.Toolkit;
And the method code is below. The solution is to create a listener and add it to the button. The listener should simply get the contents of the clipboard and insert it to whatever component you wish.
public void setClipboardContents( String aString ){
StringSelection stringSelection = new StringSelection( aString );
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents( stringSelection, this );
}
public String getClipboardContents() {
String result = "";
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//odd: the Object param of getContents is not currently used
Transferable contents = clipboard.getContents(null);
boolean hasTransferableText =
(contents != null) &&
contents.isDataFlavorSupported(DataFlavor.stringFlavor)
;
if ( hasTransferableText ) {
try {
result = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch (UnsupportedFlavorException ex){
//highly unlikely since we are using a standard DataFlavor
System.out.println(ex);
ex.printStackTrace();
}
catch (IOException ex) {
System.out.println(ex);
ex.printStackTrace();
}
}
return result;
}
Use the Actions provided by the editor kits:
JButton paste = new JButton( new DefaultEditorKit.PasteAction() );

Categories