With a label in the column heading of a column is easier:
- Add a label in the column heading graph (if the column contains text as a heading, delete it).
- Set a shortcut to allow text to flow around (Scene Builder → go to Label Properties and select Wrap Text or in Code → label.setWrapText (true))
- Set the width and height of the label.
Note. This is easier to do in FXML (using Scene Builder) than in code.
Example below:
public class TableColumnLongTextLabel extends Application { @Override public void start(Stage stage) { TableView table = new TableView(); TableColumn tableColumnData1 = new TableColumn(), tableColumnData2 = new TableColumn("Long Title without Text Wrap"); tableColumnData1.setCellValueFactory(new PropertyValueFactory<SomeStructure, String>("data1")); tableColumnData2.setCellValueFactory(new PropertyValueFactory<SomeStructure, String>("data2")); table.getColumns().addAll(tableColumnData1, tableColumnData2); Label columnTitle = new Label("Long Title with Text Wrapped"); columnTitle.setPrefWidth(125); columnTitle.setPrefHeight(50); columnTitle.setWrapText(true); columnTitle.setTextAlignment(TextAlignment.CENTER); tableColumnData1.setGraphic(columnTitle); table.setItems(FXCollections.observableArrayList( new SomeStructure("Java", "FX", 1), new SomeStructure("Java", "Swing", 0), new SomeStructure("Java", "Sample", 2), new SomeStructure("Some", "Other", 10) )); Pane layout = new VBox(10); layout.getChildren().addAll(table); stage.setScene(new Scene(layout, 350, 350)); stage.show(); } public static class SomeStructure { private final SimpleStringProperty data1; private final SimpleStringProperty data2; private final SimpleIntegerProperty data3; private SomeStructure(String data1, String data2, Integer data3) { this.data1 = new SimpleStringProperty(data1); this.data2 = new SimpleStringProperty(data2); this.data3 = new SimpleIntegerProperty(data3); } public String getData1() { return data1.get(); } public void setData1(String data1) { this.data1.set(data1); } public String getData2() { return data2.get(); } public void setData2(String data2) { this.data2.set(data2); } public Integer getData3() { return data3.get(); } public void setData3(Integer data3) { this.data3.set(data3); } } public static void main(String[] args) { launch(args); } }
Example of using FXML (no code required :)):
<TableView prefHeight="251.0" prefWidth="556.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <columns> <TableColumn editable="false" maxWidth="90.0" prefWidth="70.0" sortable="false" text="Player"> <columns> <TableColumn editable="false" maxWidth="80.0" prefWidth="50.0" text="ID" /> <TableColumn editable="false" maxWidth="200.0" prefWidth="180.0" text="Name" /> </columns> </TableColumn> <TableColumn maxWidth="80.0" minWidth="50.0" prefWidth="60.0" text="Game" /> <TableColumn editable="false" maxWidth="160.0" minWidth="125.0" prefWidth="135.0" sortable="false" text="Team" visible="false" /> <TableColumn editable="false" maxWidth="160.0" minWidth="107.0" prefWidth="107.0" sortable="false"> <graphic> <Label alignment="CENTER" text="Individual Points (Big Team)" textAlignment="CENTER" wrapText="true" /> </graphic> </TableColumn> <TableColumn editable="false" maxWidth="160.0" minWidth="99.0" prefWidth="102.0" sortable="false"> <graphic> <Label alignment="CENTER" text="Team Points (Small Maps)" textAlignment="CENTER" wrapText="true" /> </graphic> </TableColumn> <TableColumn editable="false" maxWidth="116.0" minWidth="55.0" prefWidth="55.0" sortable="false"> <graphic> <Label text="Total Points" textAlignment="CENTER" wrapText="true" /> </graphic> </TableColumn> </columns> </TableView>

source share