How can I read the X11 clipboard from a JavaFX application?

强颜欢笑 提交于 2019-12-14 01:17:34

问题


I'm working on a JavaFX application and I would like to support pasting text via middle mouse button in an X11 environment.

Is there a way to access the content of the X11 clipboard in Java/JavaFX?


回答1:


To access X11 selection clipboard:

Required imports:

import sun.awt.X11.XClipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;

Code:

XClipboard xClipboard = new XClipboard("Selection", "PRIMARY");
// Set value:
xClipboard.setContents(new StringSelection("Hello"), null);
// Read value:
String stored = xClipboard.getData(DataFlavor.stringFlavor).toString();

The important note is the values Selection and PRIMARY, as if you write:

clipboard = new XClipboard("System", "CLIPBOARD");

you will be reading the system clipboard instead.




回答2:


According to this https://bugs.openjdk.java.net/browse/JDK-8088117 there is no way to access Selection with JavaFX.

Bug report

Using JavaFX-8 with Java8u45 on SUSE11, there is no possibility to copy/paste using mouse selection or using a click on the scroll. See details here: http://stackoverflow.com/questions/30032290/javafx-mouse-clipboard-does-not-work-in-unix

Answer

Lowering the priority to P4. I note that this is more of an unimplemented feature than a bug (it has never been supported in FX). We will look at it for JDK 9. Workaround: use copy/paste (CTRL-C / CTRL-V)



来源:https://stackoverflow.com/questions/21844255/how-can-i-read-the-x11-clipboard-from-a-javafx-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!