-
-
Save nickname55/209d52d386b2f2119b0f1bb4ca32720f 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); } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment