如何将文本字段插入javafx表格视图标题?

如何将文本字段插入javafx表格视图标题?,java,javafx-2,Java,Javafx 2,我有一个简单的javafx示例,它有一个tableView。我想在列标题标签下插入一个文本字段以过滤表数据 我找到了一个例子,但它插入了文本字段而不是标题标签,而我希望同时包含标签和文本字段: import javafx.application.Application; import javafx.beans.property.SimpleStringProperty; import javafx.collections.FXCollections; import javafx.scene.No

我有一个简单的javafx示例,它有一个
tableView
。我想在列标题标签下插入一个文本字段以过滤表数据

我找到了一个例子,但它插入了文本字段而不是标题标签,而我希望同时包含标签和文本字段:

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.scene.layout.VBox;
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"));
        TableColumn searchCol = new TableColumn<>();

        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.application.application;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.collections.FXCollections;
导入javafx.scene.Node;
导入javafx.scene.scene;
导入javafx.scene.control.*;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.layout.StackPane;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类TableWithTextHeaders扩展了应用程序{
公共静态void main(字符串[]args){launch(args);}
@凌驾
公众假期开始(阶段){
TableColumn firstNameCol=新的TableColumn(“名字”);
firstNameCol.setCellValueFactory(新属性ValueFactory(“firstName”));
TableColumn lastNameCol=新的TableColumn(“姓氏”);
lastNameCol.setCellValueFactory(新属性ValueFactory(“lastName”));
TableColumn searchCol=新建TableColumn();
TableView table=新TableView();
table.getColumns().addAll(firstNameCol,lastNameCol);
表.setItems(FXCollections.observableArrayList(
新人(“雅各布”、“史密斯”),
新人(“伊莎贝拉”、“约翰逊”),
新人(“伊桑”、“威廉姆斯”)
));
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_策略);
StackPane布局=新建StackPane();
布局。设置样式(“-fx填充:10;”);
layout.getChildren().add(表);
场景=新场景(布局);
舞台场景;
stage.show();
对于(节点n:table.lookupAll(“.column header>.label”)){
if(n个标签实例){
标签=(标签)n;
TextField TextField=新的TextField(label.getText());
label.textProperty().bind(textField.textProperty());
label.setGraphic(textField);
label.setContentDisplay(仅限ContentDisplay.GRAPHIC_);
}
}
}  
公共静态类人员{
私有最终SimpleStringProperty名字;
私有最终SimpleStringProperty姓氏;
私人(字符串fName,字符串lName){
this.firstName=新的SimpleStringProperty(fName);
this.lastName=新的SimpleStringProperty(lName);
}
公共字符串getFirstName(){return firstName.get();}
public void setFirstName(字符串fName){firstName.set(fName);}
公共字符串getLastName(){返回lastName.get();}
public void setLastName(字符串fName){lastName.set(fName);}
}
}

但是,我想对一些列执行此操作,而不是对所有列。如何做到这一点?

您不需要在查找时弄脏手。只需在
表格列上设置一个图形即可:

TextField headerTextField = new TextField();
Label label = new Label("First Name");
VBox headerGraphic = new VBox();
headerGraphic.setAlignment(Pos.CENTER);
headerGraphic.getChildren().addAll(label, headerTextField);

TableColumn<Person, String> firstNameCol = new TableColumn<>();
firstNameCol.setGraphic(headerGraphic);
TextField headerTextField=newtextfield();
标签=新标签(“名字”);
VBox headerGraphic=新的VBox();
头部图形设置对齐(位置中心);
headerGraphic.getChildren().addAll(标签,headerTextField);
TableColumn firstNameCol=新TableColumn();
firstNameCol.setGraphic(headerGraphic);

你到底在问什么?预期的行为是什么?我有一个包含一些数据的tableView组件。我想在表中每列的标签底部插入一个文本字段,以根据该列筛选表的数据。现在我该怎么做(如何将这个文本字段插入到列的标题)??非常感谢我的朋友。效果非常好。祝你好运