2015-01-06 45 views
0

我有一个简单的javafx样本,它有一个tableView。我想在列标题标签下插入一个文本字段来过滤表格数据。如何将文本字段插入到javafx表视图标题中?

我找到一个这样的例子,但它插入文本字段而不是标题标签,而我想同时具有标签和文本字段。

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

但是,我想这样做的一些列并非所有。这个怎么做。

非常感谢...

+0

你问的是什么?什么是满意的行为? –

+0

我有一个包含一些数据的tableView组件。我想插入一个textField到表格中每列的标签的底部,以便根据该列过滤表格的数据。现在我怎么能做到这一点(如何将这个文本字段插入列标题)? – hossein

回答

5

您不需要在查找时弄脏手。只需在TableColumn上设置一个图形:

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

非常非常非常非常感谢我的朋友。他工作得很好。祝你好运 – hossein

相关问题