我可以在javafx 2的TableView标头列上放置TextField吗 [英] Can I place TextField on TableView header-column on javafx 2

查看:78
本文介绍了我可以在javafx 2的TableView标头列上放置TextField吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javafx的新手.谁能帮我在TableView标头上添加一些TextField.我尝试将TableView放在stackpane上,并在CSS集成的帮助下在其上方放置文本字段.不能成功.

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.

推荐答案

是的,您可以这样做.

对于JavaFX 2.2

在JavaFX 2.2(jdk7u6 +)中,实现了jira RT-14909 ,允许您在列上设置图形(任意节点)以指定该列的表标题.

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);

对于JavaFX 2.0和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.

以下是示例:

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); }
  }
}

这篇关于我可以在javafx 2的TableView标头列上放置TextField吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆