Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;}
}
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