Created
August 6, 2014 21:42
-
-
Save mansueli/1806103bf9873adaba0d to your computer and use it in GitHub Desktop.
Allow user to copy data from TableView - http://stackoverflow.com/questions/25170119/allow-user-to-copy-data-from-tableview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.beans.property.SimpleStringProperty; | |
import javafx.beans.property.StringProperty; | |
public class DataRow { | |
private Integer numSlices; | |
private StringProperty selected; | |
public DataRow(int numSlices, String selected) { | |
this.selected = new SimpleStringProperty(selected); | |
this.numSlices = numSlices; | |
} | |
public Integer getNumSlices() {return numSlices;} | |
public void setNumSlices(int num){this.numSlices = num;} | |
public void setSelected(String value) {selected.set(value);} | |
public String getSelected() {return selected.get();} | |
public StringProperty selectedProperty(){return selected;} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.scene.Scene; | |
import javafx.scene.control.SelectionMode; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
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.layout.VBox; | |
import javafx.stage.Stage; | |
public class JavaFX extends Application { | |
TableView<DataRow> table = new TableView<DataRow>(); | |
TableColumn<DataRow, Integer> numColumn; | |
TableColumn<DataRow, String> selectionColumn; | |
ObservableList<DataRow> masterData = FXCollections.observableArrayList(); | |
@Override | |
public void start(Stage stage) throws Exception { | |
VBox root = new VBox(); | |
initializeGUI(); | |
addDummyData(); | |
root.getChildren().addAll(table); | |
Scene scene = new Scene(root); | |
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); | |
} | |
}); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
@SuppressWarnings("unchecked") | |
public void initializeGUI() { | |
numColumn = new TableColumn<DataRow, Integer>(); | |
numColumn.setCellValueFactory(new PropertyValueFactory<DataRow, Integer>("numSlices")); | |
selectionColumn = new TableColumn<DataRow, String>(); | |
selectionColumn.setCellValueFactory(new PropertyValueFactory<DataRow, String>("selected")); | |
selectionColumn.setEditable(true); | |
table.getColumns().addAll(numColumn, selectionColumn); | |
table.setEditable(true); | |
table.getSelectionModel().setCellSelectionEnabled(true); | |
table.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); | |
} | |
public void addDummyData() { | |
for (int i = 0; i < 13; i++) | |
masterData.add(new DataRow(i, "Text " + i)); | |
table.getItems().addAll(masterData); | |
} | |
public static void main(String[] args) throws Exception { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment