Skip to content

Instantly share code, notes, and snippets.

@Roland09
Created February 11, 2015 18:07
Show Gist options
  • Save Roland09/0c7e26a2e7f72584fc1d to your computer and use it in GitHub Desktop.
Save Roland09/0c7e26a2e7f72584fc1d to your computer and use it in GitHub Desktop.
Example about copying multiple selected table cells to the clipboard. Select the cells and press CTRL+C. The clipboard data can then be pasted into Excel, Notepad, etc. Copy works, Paste operation will be added in a separate gist.
import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class TableCopyCellsDemo extends Application {
private final ObservableList<Person> data = FXCollections.observableArrayList(new Person("Jacob", "Smith", 18), new Person("Isabella", "Johnson", 19), new Person("Ethan", "Williams", 20), new Person("Michael", "Brown", 21));
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
stage.setWidth(500);
stage.setHeight(550);
// create table columns
TableColumn<Person, String> firstNameCol = new TableColumn<Person, String>("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
TableColumn<Person, String> lastNameCol = new TableColumn<Person, String>("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
TableColumn<Person, Integer> ageCol = new TableColumn<Person, Integer>("Age");
ageCol.setMinWidth(60);
ageCol.setCellValueFactory(new PropertyValueFactory<Person, Integer>("age"));
TableView<Person> table = new TableView<>();
table.setPlaceholder(new Text("No content in table"));
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, ageCol);
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 10, 10, 10));
BorderPane borderPane = new BorderPane();
borderPane.setCenter(table);
vbox.getChildren().addAll(borderPane);
vbox.getChildren().add( new Label( "Select cells and press CTRL+C. Paste the data into Excel or Notepad"));
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.show();
// enable multi-selection
table.getSelectionModel().setCellSelectionEnabled(true);
table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
// enable copy/paste
TableUtils.installCopyPasteHandler(table);
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleIntegerProperty age;
private Person(String fName, String lName, Integer age) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.age = new SimpleIntegerProperty(age);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String fName) {
lastName.set(fName);
}
public Integer getAge() {
return age.get();
}
public void setAge(Integer fName) {
age.set(fName);
}
}
}
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.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
public class TableUtils {
/**
* Install the keyboard handler:
* + CTRL + C = copy to clipboard
* @param table
*/
public static void installCopyPasteHandler(TableView<?> table) {
// install copy/paste keyboard handler
table.setOnKeyPressed(new TableKeyEventHandler());
}
/**
* Copy/Paste keyboard event handler.
* The handler uses the keyEvent's source for the clipboard data. The source must be of type TableView.
*/
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) {
// copy to clipboard
copySelectionToClipboard( (TableView<?>) keyEvent.getSource());
System.out.println("Selection copied to clipboard");
// event is handled, consume it
keyEvent.consume();
}
}
}
}
/**
* Get table selection and copy it to the clipboard.
* @param table
*/
public static void copySelectionToClipboard(TableView<?> table) {
StringBuilder clipboardString = new StringBuilder();
ObservableList<TablePosition> positionList = table.getSelectionModel().getSelectedCells();
int prevRow = -1;
for (TablePosition position : positionList) {
int row = position.getRow();
int col = position.getColumn();
Object cell = (Object) table.getColumns().get(col).getCellData(row);
// null-check: provide empty string for nulls
if (cell == null) {
cell = "";
}
// determine whether we advance in a row (tab) or a column
// (newline).
if (prevRow == row) {
clipboardString.append('\t');
} else if (prevRow != -1) {
clipboardString.append('\n');
}
// create string from cell
String text = cell.toString();
// add new item to clipboard
clipboardString.append(text);
// remember previous
prevRow = row;
}
// create clipboard content
final ClipboardContent clipboardContent = new ClipboardContent();
clipboardContent.putString(clipboardString.toString());
// set clipboard content
Clipboard.getSystemClipboard().setContent(clipboardContent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment