PropertyValueFactory javafx

public class PropertyValueFactory<S,T>
extends java.lang.Object

implements Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>>


A convenience implementation of the Callback interface, designed specifically for use within the TableColumn cell value factory. An example of how to use this class is:
 TableColumn<Person,String> firstNameCol = new TableColumn<Person,String>("First Name");
 firstNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("firstName"));
 
In this example, the "firstName" string is used as a reference to an assumed firstNameProperty() method in the Person class type (which is the class type of the TableView items list). Additionally, this method must return a Property instance. If a method meeting these requirements is found, then the TableCell is populated with this ObservableValue. In addition, the TableView will automatically add an observer to the returned value, such that any changes fired will be observed by the TableView, resulting in the cell immediately updating.
If no method matching this pattern exists, there is fall-through support for attempting to call get<property>() or is<property>() (that is, getFirstName() or isFirstName() in the example above). If a method matching this pattern exists, the value returned from this method is wrapped in a ReadOnlyObjectWrapper and returned to the TableCell. However, in this situation, this means that the TableCell will not be able to observe the ObservableValue for changes (as is the case in the first approach above).


For reference (and as noted in the TableColumn cell value factory documentation), the long form of the code above would be the following:
TableColumn<Person,String> firstNameCol = new TableColumn<Person,String>("First Name");
 firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<Person, String>, ObservableValue<String>>() {
     public ObservableValue<String> call(CellDataFeatures<Person, String> p) {
         // p.getValue() returns the Person instance for a particular TableView row
         return p.getValue().firstNameProperty();
     }
  });
 }


 
See Also:
TableColumn, TableView, TableCell, MapValueFactory
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JavaFX的TableView是一个用于显示和编辑表格数据的UI组件。它可以用于展示数据集合,并且支持对数据进行排序、过滤和编辑操作。要使用TableView,你需要创建一个表格模型,并将其与TableView绑定。下面是一个简单的示例代码,展示如何使用TableView: ```java import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.TableColumn;import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; import javafx.stage.Stage; public class TableViewExample extends Application { @Override public void start(Stage primaryStage) { // 创建表格和列对象 TableView<Person> tableView = new TableView<>(); TableColumn<Person, String> nameCol = new TableColumn<>("Name"); TableColumn<Person, Integer> ageCol = new TableColumn<>("Age"); // 配置列和数据绑定 nameCol.setCellValueFactory(new PropertyValueFactory<>("name")); ageCol.setCellValueFactory(new PropertyValueFactory<>("age")); // 添加列到表格 tableView.getColumns().addAll(nameCol, ageCol); // 创建数据集合 ObservableList<Person> data = FXCollections.observableArrayList( new Person("John", 25), new Person("Jane", 30), new Person("Bob", 35) ); // 将数据集合设置给表格 tableView.setItems(data); // 创建场景和舞台 Scene scene = new Scene(tableView); primaryStage.setScene(scene); primaryStage.show(); } public static class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } public static void main(String[] args) { launch(args); } } ``` 这个示例创建了一个简单的TableView,其中包含两列:Name和Age。数据集合使用ObservableList来存储Person对象,并将其与表格绑定。你可以根据需要自定义列的样式、宽度和行为。 请问还有其他

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值