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


Question

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.

1
2
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");
col.setGraphic(colHeaderTextField);

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.beans.property.SimpleStringProperty;
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);
    table.setItems(FXCollections.observableArrayList(
      new Person("Jacob", "Smith"),
      new Person("Isabella", "Johnson"),
      new Person("Ethan", "Williams")
    ));
    table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

    StackPane layout = new StackPane();
    layout.setStyle("-fx-padding: 10;");
    layout.getChildren().add(table);
    Scene scene = new Scene(layout);
    stage.setScene(scene);
    stage.show();

    for (Node n: table.lookupAll(".column-header > .label")) {
      if (n instanceof Label) {
        Label label = (Label) n;
        TextField textField = new TextField(label.getText());
        label.textProperty().bind(textField.textProperty());
        label.setGraphic(textField);
        label.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
      }
    }
  }  

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

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