javafx - 如何从嵌套的 TableColumn 访问父值
问题描述
我有一个表 ( TableView
),其中经常重复一组列(例如,姓名和地址)。我将这些列分组为公共列的子列
<TableColumn text="Получател">
<columns>
<TableColumn text="ЕИК"/>
<TableColumn text="ЗДДС №"/>
<TableColumn text="Име"/>
<TableColumn text="Адрес"/>
<TableColumn text="Материално отговорно лице"/>
<TableColumn text="Получател"/>
</columns>
</TableColumn>
我将这组列导出到用户控件
<fx:root type="com.example.CompanyTableColumn" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml"
text="${controller.name}">
<columns>
<TableColumn fx:id="eikColumn" text="ЕИК"/>
<TableColumn fx:id="vatColumn" text="ЗДДС №"/>
<TableColumn fx:id="nameColumn" text="Име"/>
<TableColumn fx:id="addressColumn" text="Адрес"/>
<TableColumn text="Материално отговорно лице"/>
</columns>
</fx:root>
public class CompanyTableColumn extends TableColumn<Invoice, Company> {
@FXML
private TableColumn<Invoice, String> eikColumn;
@FXML
private TableColumn<Invoice, String> vatColumn;
@FXML
private TableColumn<Invoice, String> nameColumn;
@FXML
private TableColumn<Invoice, String> addressColumn;
private StringProperty name = new SimpleStringProperty();
public CompanyTableColumn() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/com/example/table_column_company.fxml"));
loader.setRoot(this);
loader.setController(this);
loader.load();
}
catch (IOException e) {
e.printStackTrace();
}
}
@FXML
private void initialize() {
}
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
}
通过如下初始化用户控件
<TableView fx:id="tableView">
<columns>
<CompanyTableColumn fx:id="supplierColumn" name="Доставчик"/>
<CompanyTableColumn fx:id="receiverColumn" name="Получател"/>
<columns>
</TableView>
supplierColumn.setCellValueFactory(cdf -> new SimpleObjectProperty<>(cdf.getValue().getSupplier()));
receiverColumn.setCellValueFactory(cdf -> new SimpleObjectProperty<>(cdf.getValue().getReceiver()));
我的问题来了。在示例中,两个控件必须可视化来自Company
模型不同实例的数据。我通过创建 CellValueFactory 提交各种实例,但我看不到如何访问特定值,CompanyTableColumn
因此我可以在创建嵌套列值的工厂中使用它。
编辑
像往常一样,在提出问题后不久就找到了解决方案:
@FXML
private void initialize() {
eikColumn.setCellValueFactory(cdf -> new SimpleStringProperty(getCellObservableValue(cdf.getValue()).getValue().getEik()));
vatColumn.setCellValueFactory(cdf -> new SimpleStringProperty(getCellObservableValue(cdf.getValue()).getValue().getVat()));
nameColumn.setCellValueFactory(cdf -> new SimpleStringProperty(getCellObservableValue(cdf.getValue()).getValue().getName()));
addressColumn.setCellValueFactory(cdf -> new SimpleStringProperty(getCellObservableValue(cdf.getValue()).getValue().getAddress()));
}
解决方案
The solution I found consisted of calling TableColumn::getCellObservableValue
using the value corresponding to the corresponding row in the table as a parameter (returned by the CellDataFeatures
object used in the callback function to generate a value)
public class CompanyTableColumn extends TableColumn<Invoice, Company> {
@FXML
private TableColumn<Invoice, String> eikColumn;
@FXML
private TableColumn<Invoice, String> vatColumn;
@FXML
private TableColumn<Invoice, String> nameColumn;
@FXML
private TableColumn<Invoice, String> addressColumn;
private StringProperty name = new SimpleStringProperty();
public CompanyTableColumn() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/com/example/table_column_company.fxml"));
loader.setRoot(this);
loader.setController(this);
loader.load();
}
catch (IOException e) {
e.printStackTrace();
}
}
@FXML
private void initialize() {
eikColumn.setCellValueFactory(cdf -> new SimpleStringProperty(getCellObservableValue(cdf.getValue()).getValue().getEik()));
vatColumn.setCellValueFactory(cdf -> new SimpleStringProperty(getCellObservableValue(cdf.getValue()).getValue().getVat()));
nameColumn.setCellValueFactory(cdf -> new SimpleStringProperty(getCellObservableValue(cdf.getValue()).getValue().getName()));
addressColumn.setCellValueFactory(cdf -> new SimpleStringProperty(getCellObservableValue(cdf.getValue()).getValue().getAddress()));
}
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
}
推荐阅读
- mysql - MySQL 事件可以立即删除多行吗?
- java - Java Spring Boot 覆盖注释错误
- ios - 如何在 SwiftUI 上登录 Facebook?
- r - Windows 的 conda 环境中的 r-base 错误
- python - 如何在快速的网络服务器上托管 CNN 神经网络?
- python - 从完全限定路径创建和写入文本文件
- java - 如何在碰撞后或经过一定时间后销毁子弹 LibGDX Box2d
- speech-recognition - 调试自定义词PocketSphinx.js的语音识别
- php - PHP递归函数通过引用传递
- python - 并行化 Dask 聚合