Allow user to copy data from TableView

I have a simple JavaFX application that allows a user to query a database and view data in a table.

I want the user to be able to click on a table cell and copy the text from this cell to the clipboard with a standard bar as a clipboard: ctrl-c for Win / Linux or cmd-c for Mac. FYI, text input elements support the base copy / paste by default.

I am using the standard javafx.scene.control.TableView class. Is there an easy way to make a copy of a cell? I did some searches, and I see other people creating custom menu commands ... I don't want to create a custom menu, I just want the main keyboard copy to work with individual cells.

I use single selection mode, but if necessary, I can change something else:

TableView<Document> tableView = new TableView<Document>(); tableView.getSelectionModel().setCellSelectionEnabled(true); tableView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); 
+6
source share
2 answers

You just need to create a listener in the scene, something like:

 scene.getAccelerators() .put(new KeyCodeCombination(KeyCode.C, KeyCombination.CONTROL_ANY), new Runnable() { @Override public void run() { int row = table.getSelectionModel().getSelectedIndex(); DataRow tmp = table.getItems().get(row); final Clipboard clipboard = Clipboard.getSystemClipboard(); final ClipboardContent content = new ClipboardContent(); if(table.getSelectionModel().isSelected(row, numColumn)){ System.out.println(tmp.getNumSlices()); content.putString(tmp.getNumSlices().toString()); } else{ System.out.println(tmp.getSelected()); content.putString(tmp.getSelected()); } clipboard.setContent(content); } }); 

As a complete example, you can upload it to gist .

+2
source

I recommended you review this post, work for me

http://respostas.guj.com.br/47439-habilitar-copypaste-tableview-funcionando-duvida-editar-funcionalidade

The author uses an additional java class to include the contents of the contents of a cell from a tableView

+1
source

Source: https://habr.com/ru/post/973462/


All Articles