答案 0 :(得分:1)
您需要为TableColumn
提供Cell Factory和Cell Value Factory。
cellFactory负责渲染从cellValueFactory获取的数据。
TableColumn<YourDataModel, String> yourColumn = new TableColumn<>();
yourColumn.setCellFactory(tableColumn -> new DownloadCell());
yourColumn.setCellValueFactory(cellData -> cellData.getValue().downloadProperty());
public class YourDataModel {
private StringProperty download = new SimpleStringProperty();
// additional fields
public StringProperty downloadProperty() {
return download;
}
public String getDownload() {
return download.get();
}
public void setDownload(String value) {
download.set(value);
}
}
public class DownloadCell extends TableCell<YourDataModel, String> {
private Hyperlink downloadLink;
public DownloadCell() {
downloadLink = new Hyperlink();
downloadLink.setOnAction(evt -> {
try {
Desktop.getDesktop().browse(new URI(downloadLink.getText()));
} catch (Exception e) {
// exception handling
}
});
}
@Override
protected void updateItem(String link, boolean empty) {
super.updateItem(link, empty);
if (link == null || empty) {
setGraphic(null);
} else {
downloadLink.setText(link);
setGraphic(downloadLink);
}
}
}