Skip to content

Instantly share code, notes, and snippets.

@jewelsea
Last active September 9, 2022 22:59
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jewelsea/2898196 to your computer and use it in GitHub Desktop.
Save jewelsea/2898196 to your computer and use it in GitHub Desktop.
JavaFX sample tableview with wrapped headers.
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
public class TableWrappedHeaders extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) {
TableColumn firstNameCol = new TableColumn("First Name (which is a really long name)");
makeHeaderWrappable(firstNameCol);
firstNameCol.setPrefWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("firstName"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setPrefWidth(100);
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("lastName"));
TableView table = new TableView();
table.getColumns().addAll(firstNameCol, lastNameCol);
table.setItems(FXCollections.observableArrayList(
new Person("Jacob", "Smith"),
new Person("Isabella", "Johnson"),
new Person("Ethan", "Williams")
));
table.setPrefSize(250, 200);
Pane layout = new VBox(10);
layout.setStyle("-fx-padding: 10;");
layout.getChildren().addAll(table);
stage.setScene(new Scene(layout));
stage.show();
}
private void makeHeaderWrappable(TableColumn col) {
Label label = new Label(col.getText());
label.setStyle("-fx-padding: 8px;");
label.setWrapText(true);
label.setAlignment(Pos.CENTER);
label.setTextAlignment(TextAlignment.CENTER);
StackPane stack = new StackPane();
stack.getChildren().add(label);
stack.prefWidthProperty().bind(col.widthProperty().subtract(5));
label.prefWidthProperty().bind(stack.prefWidthProperty());
col.setText(null);
col.setGraphic(stack);
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private Person(String fName, String lName) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
}
public String getFirstName() { return firstName.get(); }
public void setFirstName(String fName) { firstName.set(fName); }
public String getLastName() { return lastName.get(); }
public void setLastName(String fName) { lastName.set(fName); }
}
}
Copy link

ghost commented May 30, 2016

Excelente estaba utilizando un jtreetableview y por fin pude alinear.. gracias

@idonava
Copy link

idonava commented Dec 3, 2018

Thanks!

@abryantsev
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment