JavaFX: Mouse clipboard does not work in Unix

2019-07-20 18:21发布

问题:

In Unix, as you probably know, it is common to use the mouse for copy-paste operations. I.e. text selection automatically copies it to the clipboard and clicking on the scroll pastes it where you click.

I do not have clipboard problems with GUI applications that use Java 7 with Swing.

But in JavaFX with Java 8 (update 45), the mouse clipboard does not work, so that:

  • I cannot paste any text to the GUI by clicking the scroll
  • I cannot copy any text from the GUI by just selecting it

I encountered this problem with TextInputDialog and with TextArea. I haven't tried other widgets so far.

Can you please tell how to enable the mouse clipboard?

Screenshots:

TextInputDialogScreenshot http://oi59.tinypic.com/smr49c.jpg

TextAreaScreenshot http://oi57.tinypic.com/1zwzvyp.jpg

回答1:

Hello I would like to help with your own code but I did not see any so I have an example of something I did.

@FXML
    private void handleButtonAction21(ActionEvent event) throws IOException {
            ObservableList<Part> rowList = table2.getSelectionModel().getSelectedItems();

            final ClipboardContent content = new ClipboardContent();

            content.putString(rowList.get(0).getPartNumber());
            Clipboard.getSystemClipboard().setContent(content);
    }

this is an event listener for a context menu item.

Basically you would have to control the context menu and then get the SystemClipboard and set the text in there



回答2:

In the moment there is no supported way to do this with JDK.
The bug/feature request is listed here
[OpenJDK bug report: https://bugs.openjdk.java.net/browse/JDK-8088117]
It backlinks now to this SO entry.

@user4231709 Sorry, your solution doesn't help with the problem because this uses the system clipboard which is different. For further reading look for "sun.awt.X11.XSelection".

There is also another SO entry about this topic.