Table headers do not align with their content when first displayed if css is applied to style the table cells.
If table.requestLayout(); is called after the table is shown, then the headers and cells automatically line up as expected.
----
Sample code . . .
/** file: cell-shader.css
place in same directory as CellShadedTable.java */
.italic.table-cell { -fx-font-family: "Times New Roman"; -fx-font-style: italic; }
/** file: CellShadedTable.java */
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
// demonstrates styling column cells in a tableview.
public class CellShadedTable extends Application {
public static void main(String[] args) throws Exception {
launch(args);
}
@Override
public void start(final Stage stage) throws Exception {
stage.setTitle("So called friends . . .");
// create a table.
TableColumn<Friend, String> nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
nameColumn.setPrefWidth(75);
nameColumn.getStyleClass().add("italic");
TableColumn<Friend, String> owesMeColumn = new TableColumn<>("Owes Me");
owesMeColumn.setCellValueFactory(new PropertyValueFactory<>("owesMe"));
owesMeColumn.setPrefWidth(150);
TableColumn<Friend, Boolean> willPayColumn = new TableColumn<>("Will Pay Up");
willPayColumn.setCellValueFactory(new PropertyValueFactory<>("willPay"));
willPayColumn.setPrefWidth(75);
TableView<Friend> table = new TableView<>(Friend.data);
table.getColumns().addAll(
nameColumn,
owesMeColumn,
willPayColumn
);
table.setPrefHeight(200);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
stage.setScene(new Scene(table));
stage.getScene().getStylesheets().add(getClass().getResource("cell-shader.css").toExternalForm());
stage.show();
// uncomment this line and table headers and columns match up.
// table.requestLayout();
}
/**
* Sample data for a table view
*/
public static class Friend {
final static public ObservableList<Friend> data = FXCollections.observableArrayList(
new Friend("George", "Movie Ticket", true),
new Friend("Irene", "Pay Raise", false),
new Friend("Ralph", "Return my Douglas Adams Books", false),
new Friend("Otto", "Game of Golf", true),
new Friend("Sally", "$12,045.98", false),
new Friend("Antoine", "Latte", true)
);
final private String name;
final private String owesMe;
final private boolean willPay;
public Friend(String name, String owesMe, boolean willPay) {
this.name = name;
this.owesMe = owesMe;
this.willPay = willPay;
}
public String getName() {
return name;
}
public String getOwesMe() {
return owesMe;
}
public boolean getWillPay() {
return willPay;
}
}
}
If table.requestLayout(); is called after the table is shown, then the headers and cells automatically line up as expected.
----
Sample code . . .
/** file: cell-shader.css
place in same directory as CellShadedTable.java */
.italic.table-cell { -fx-font-family: "Times New Roman"; -fx-font-style: italic; }
/** file: CellShadedTable.java */
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
// demonstrates styling column cells in a tableview.
public class CellShadedTable extends Application {
public static void main(String[] args) throws Exception {
launch(args);
}
@Override
public void start(final Stage stage) throws Exception {
stage.setTitle("So called friends . . .");
// create a table.
TableColumn<Friend, String> nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
nameColumn.setPrefWidth(75);
nameColumn.getStyleClass().add("italic");
TableColumn<Friend, String> owesMeColumn = new TableColumn<>("Owes Me");
owesMeColumn.setCellValueFactory(new PropertyValueFactory<>("owesMe"));
owesMeColumn.setPrefWidth(150);
TableColumn<Friend, Boolean> willPayColumn = new TableColumn<>("Will Pay Up");
willPayColumn.setCellValueFactory(new PropertyValueFactory<>("willPay"));
willPayColumn.setPrefWidth(75);
TableView<Friend> table = new TableView<>(Friend.data);
table.getColumns().addAll(
nameColumn,
owesMeColumn,
willPayColumn
);
table.setPrefHeight(200);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
stage.setScene(new Scene(table));
stage.getScene().getStylesheets().add(getClass().getResource("cell-shader.css").toExternalForm());
stage.show();
// uncomment this line and table headers and columns match up.
// table.requestLayout();
}
/**
* Sample data for a table view
*/
public static class Friend {
final static public ObservableList<Friend> data = FXCollections.observableArrayList(
new Friend("George", "Movie Ticket", true),
new Friend("Irene", "Pay Raise", false),
new Friend("Ralph", "Return my Douglas Adams Books", false),
new Friend("Otto", "Game of Golf", true),
new Friend("Sally", "$12,045.98", false),
new Friend("Antoine", "Latte", true)
);
final private String name;
final private String owesMe;
final private boolean willPay;
public Friend(String name, String owesMe, boolean willPay) {
this.name = name;
this.owesMe = owesMe;
this.willPay = willPay;
}
public String getName() {
return name;
}
public String getOwesMe() {
return owesMe;
}
public boolean getWillPay() {
return willPay;
}
}
}