JavaFX TableView copies to clipboard

Is it possible to allow the user to select rows and copy from TableView?

+6
source share
4 answers
tableView.getSelectionModel().setCellSelectionEnabled(true); tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); MenuItem item = new MenuItem("Copy"); item.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { ObservableList<TablePosition> posList = tableView.getSelectionModel().getSelectedCells(); int old_r = -1; StringBuilder clipboardString = new StringBuilder(); for (TablePosition p : posList) { int r = p.getRow(); int c = p.getColumn(); Object cell = tableView.getColumns().get(c).getCellData(r); if (cell == null) cell = ""; if (old_r == r) clipboardString.append('\t'); else if (old_r != -1) clipboardString.append('\n'); clipboardString.append(cell); old_r = r; } final ClipboardContent content = new ClipboardContent(); content.putString(clipboardString.toString()); Clipboard.getSystemClipboard().setContent(content); } }); ContextMenu menu = new ContextMenu(); menu.getItems().add(item); tableView.setContextMenu(menu); 
+9
source

I could not implement the Yelliver Answer, it does not compile for me, but I found another very clear way to extract several selected data to the TableView clipboard, it looks like this

 TableView tableView = new TableView(); tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); MenuItem item = new MenuItem("Copy"); item.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { ObservableList rowList = (ObservableList) tableView.getSelectionModel().getSelectedItems(); StringBuilder clipboardString = new StringBuilder(); for (Iterator it = rowList.iterator(); it.hasNext();) { ObservableList<Object> row = (ObservableList<Object>) it.next(); for (Object cell : row) { if (cell == null) { cell = ""; } clipboardString.append(cell); clipboardString.append('\t'); } clipboardString.append('\n'); } final ClipboardContent content = new ClipboardContent(); content.putString(clipboardString.toString()); Clipboard.getSystemClipboard().setContent(content); } }); ContextMenu menu = new ContextMenu(); menu.getItems().add(item); tableView.setContextMenu(menu); } 

Hope this helps you or anyone trying to easily copy TableView data.

+3
source

The yelliver solution only copies the contents of the selected cells, but apparently only those cells that were explicitly pressed are selected. The Roberto solution only works if the objects stored in the table are iterable. Here is a general solution that copies data from all cells in all selected rows:

 @SuppressWarnings("rawtypes") public void copySelectionToClipboard(final TableView<?> table) { final Set<Integer> rows = new TreeSet<>(); for (final TablePosition tablePosition : table.getSelectionModel().getSelectedCells()) { rows.add(tablePosition.getRow()); } final StringBuilder strb = new StringBuilder(); boolean firstRow = true; for (final Integer row : rows) { if (!firstRow) { strb.append('\n'); } firstRow = false; boolean firstCol = true; for (final TableColumn<?, ?> column : table.getColumns()) { if (!firstCol) { strb.append('\t'); } firstCol = false; final Object cellData = column.getCellData(row); strb.append(cellData == null ? "" : cellData.toString()); } } final ClipboardContent clipboardContent = new ClipboardContent(); clipboardContent.putString(strb.toString()); Clipboard.getSystemClipboard().setContent(clipboardContent); } 

To enable copying with Ctrl + C, add

  final KeyCodeCombination keyCodeCopy = new KeyCodeCombination(KeyCode.C, KeyCombination.CONTROL_ANY); table.setOnKeyPressed(event -> { if (keyCodeCopy.match(event)) { copySelectionToClipboard(table); } }); 
+2
source

I'm not sure, but I think javafx.scene.input.Clipboard can help you.

0
source

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


All Articles