Can I place TextField on TableView header-column on javafx 2


I am newbie on javafx. Can anybody help me to add some TextFields on TableView header. I try it with put TableView on stackpane and place textfields on above it, with the help of css integration. Can't success.

5/9/2012 10:58:10 PM

Yes, you can do this.

For JavaFX 2.2

In JavaFX 2.2 (jdk7u6+) jira RT-14909 was implemented, which allows you to set a graphic (arbitrary node) on a column to specify the table header for the column.

TableColumn col = new TableColumn("");
TextField colHeaderTextField = new TextField("Pink Elephants");

For JavaFX 2.0 and 2.1

After you have added the table to an active scene and it has been rendered, you can lookup the table header labels and change them to display a graphic (which can be any kind of node) which displays textfields for the table headers rather than the default static label text.

Here is a sample:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TableWithTextHeaders extends Application {
  public static void main(String[] args) { launch(args); }

  @Override public void start(Stage stage) {
    TableColumn firstNameCol = new TableColumn("First Name");
    firstNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("firstName"));
    TableColumn lastNameCol = new TableColumn("Last Name");
    lastNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("lastName"));

    TableView table = new TableView();
    table.getColumns().addAll(firstNameCol, lastNameCol);
      new Person("Jacob", "Smith"),
      new Person("Isabella", "Johnson"),
      new Person("Ethan", "Williams")

    StackPane layout = new StackPane();
    layout.setStyle("-fx-padding: 10;");
    Scene scene = new Scene(layout);

    for (Node n: table.lookupAll(".column-header > .label")) {
      if (n instanceof Label) {
        Label label = (Label) n;
        TextField textField = new TextField(label.getText());

  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); }
7/11/2012 9:10:16 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow