作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的 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); }
}
}
但是我想对某些列执行此操作,而不是对所有列。如何做到这一点?
最佳答案
您无需亲自进行查找。只需在 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);
关于java - 如何将文本字段插入到javafx TableView 标题中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27794036/
我是一名优秀的程序员,十分优秀!