テーブルの行をテキストとしてコピーする および Drag&Dropする
リファクタリング前。
どのページを参考にしたのか忘れたので、後で追記する。
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package firstfxml.util; import java.text.NumberFormat; import javafx.beans.property.DoubleProperty; import javafx.beans.property.IntegerProperty; import javafx.beans.property.StringProperty; import javafx.collections.ObservableList; import javafx.event.EventHandler; import javafx.scene.control.TablePosition; import javafx.scene.control.TableView; import javafx.scene.input.Clipboard; import javafx.scene.input.ClipboardContent; import javafx.scene.input.Dragboard; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCodeCombination; import javafx.scene.input.KeyCombination; import javafx.scene.input.KeyEvent; import javafx.scene.input.MouseEvent; import javafx.scene.input.TransferMode; public class TableUtils { private static NumberFormat numberFormatter = NumberFormat.getNumberInstance(); public static void installCopyPasteHandler(TableView<?> table) { table.setOnKeyPressed(new TableKeyEventHandler()); table.setOnDragDetected(new TableMouseDragEventHandler2()); } public static class TableMouseDragEventHandler2 implements EventHandler<MouseEvent> { public void handle(final MouseEvent mouseEvent) { if (mouseEvent.getSource() instanceof TableView) { dragSelection((TableView<?>) mouseEvent.getSource()); mouseEvent.consume(); } } } public static class TableKeyEventHandler implements EventHandler<KeyEvent> { KeyCodeCombination copyKeyCodeCompination = new KeyCodeCombination(KeyCode.C, KeyCombination.CONTROL_ANY); public void handle(final KeyEvent keyEvent) { if (copyKeyCodeCompination.match(keyEvent)) { if (keyEvent.getSource() instanceof TableView) { copySelectionToClipboard((TableView<?>) keyEvent.getSource()); keyEvent.consume(); } } } } public static void copySelectionToClipboard(TableView<?> table) { final ClipboardContent clipboardContent = new ClipboardContent(); clipboardContent.putString(getSelection(table)); Clipboard.getSystemClipboard().setContent(clipboardContent); } public static void dragSelection(TableView<?> table) { String selection = getSelection(table); Dragboard dragboard = table.startDragAndDrop(TransferMode.COPY); ClipboardContent content = new ClipboardContent(); content.putString(selection); dragboard.setContent(content); } public static String getSelection(TableView<?> table) { StringBuilder clipboardString = new StringBuilder(); ObservableList<TablePosition> positionList = table.getSelectionModel().getSelectedCells(); int prevRow = -1; for (TablePosition position : positionList) { int row = position.getRow(); if (prevRow != -1) { clipboardString.append('\n'); } String text = ""; for (int i = 0; i < table.getColumns().size(); i++) { Object observableValue = (Object) table.getColumns().get(i).getCellObservableValue(row); if (observableValue == null) { text += ""; } else if (observableValue instanceof DoubleProperty) { text += numberFormatter.format(((DoubleProperty) observableValue).get()); } else if (observableValue instanceof IntegerProperty) { text += numberFormatter.format(((IntegerProperty) observableValue).get()); } else if (observableValue instanceof StringProperty) { text += ((StringProperty) observableValue).get(); } else { System.out.println("Unsupported observable value: " + observableValue); } if (i != table.getColumns().size() - 1) { text += "\t"; } } clipboardString.append(text); prevRow = row; } return clipboardString.toString(); } }