我有一列路径,其中所有路径都属于一个根文件夹。当路径不适合列时(它们可能很长,列宽有限),我想仅向用户显示带有前导省略号的路径末尾。例如:
..someverylongdirectoryname/file.txt
..omeverylongdirectoryname/file2.txt
我试过这个代码,但是没有用。
public class JavaFxTest extends Application {
public static class FilePath {
private final String path;
public FilePath(String path) {
this.path = path;
}
public String getPath() {
return path;
}
}
@Override
public void start(Stage primaryStage) {
TableView<FilePath> tableView = new TableView<>();
TableColumn<FilePath, String> pathColumn = new TableColumn<>("Path");
pathColumn.setCellValueFactory(new PropertyValueFactory<>("path"));
pathColumn.setPrefWidth(200);
pathColumn.setCellFactory(column -> new javafx.scene.control.TableCell<>() {
private final Text text = new Text();
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setGraphic(null);
} else {
text.setText(item);
text.setStyle("-fx-text-alignment: right;");
setGraphic(text);
}
}
});
tableView.getColumns().add(pathColumn);
tableView.getItems().addAll(
new FilePath("/usr/local/bin/someverylongdirectoryname/file.txt"),
new FilePath("/usr/local/bin/someverylongdirectoryname/file2.txt")
);
primaryStage.setScene(new Scene(tableView, 300, 200));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
有人能告诉我该怎么做吗?
看起来您希望文本超出省略号的范围位于标签的开头,而不是结尾。只需利用
Cell
是 a这一事实Labeled
并设置超出样式:当然,如果您愿意,您也可以手动替换根文件夹: