Last active
July 11, 2024 18:46
-
-
Save jewelsea/2898196 to your computer and use it in GitHub Desktop.
JavaFX sample tableview with wrapped headers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | |
} | |
} |
Excelente estaba utilizando un jtreetableview y por fin pude alinear.. gracias
Thanks!
👍
Very nice but it is very strange but a nested column doesn't behave properly.
@Override
public void start(Stage stage)
{
TableColumn nestedCol = new TableColumn("Nested Column (also very long name)");
makeHeaderWrappable(nestedCol);
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"));
nestedCol.getColumns().addAll(firstNameCol, lastNameCol);
TableView table = new TableView();
table.getColumns().addAll(nestedCol);
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();
}
When this code is started the nested column is wrapped. But when you reduce the columns by dragging the mouse then the height of the nested column stays the same.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/10952111/javafx-2-0-table-with-multiline-table-header