我想在带有超链接的表中只显示一个列单元格,单击它会在新窗口中显示一些文本。请帮帮我这个
答案 0 :(得分:2)
您需要实现自定义单元格类型才能执行此操作:
public class HyperlinkTableCell<S, T> extends TableCell<S, T> {
private final Hyperlink link;
private final Set<T> visitedLinks;
private final Function<? super T, String> converter;
private HyperlinkTableCell(Set<T> visitedLinks, final Consumer<? super T> handler, Function<? super T, String> converter) {
link = new Hyperlink();
if (handler != null) {
link.setOnAction(evt -> {
T item = getItem();
handler.accept(item);
// keep track of visited links
visitedLinks.add(item);
});
}
this.visitedLinks = visitedLinks;
this.converter = converter;
}
@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setGraphic(null);
} else {
setGraphic(link);
link.setText(converter.apply(item));
// restore visited state
link.setVisited(visitedLinks.contains(item));
}
}
public static <U, V> Callback<TableColumn<U, V>, TableCell<U, V>> forTableColumn(
final Consumer<? super V> handler,
final Function<? super V, String> converter) {
final Set<V> set = new HashSet<>();
return c -> new HyperlinkTableCell<>(set, handler, converter);
}
public static <U, V> Callback<TableColumn<U, V>, TableCell<U, V>> forTableColumn(
final Consumer<? super V> handler) {
return forTableColumn(handler, item -> item == null ? "" : item.toString());
}
}
column.setCellFactory(HyperlinkTableCell.forTableColumn(item -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setContentText("You clicked " + item);
alert.show();
}));