Eu tenho uma coluna com caminhos onde todos os caminhos pertencem a uma pasta raiz. Quando os caminhos não cabem na coluna (eles podem ser bastante longos e a largura da coluna é limitada), quero mostrar ao usuário apenas o final do caminho com reticências à esquerda. Por exemplo:
..someverylongdirectoryname/file.txt
..omeverylongdirectoryname/file2.txt
Tentei esse código, mas não funcionou.
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);
}
}
Alguém poderia dizer como fazer isso?
Parece que você deseja que o texto ultrapasse as reticências no início do rótulo, em vez de no final. Basta usar o fato de que
Cell
é aLabeled
e definir o estilo de saturação:Claro, você também pode substituir manualmente a pasta raiz, se preferir: