如何从嵌套的 TableColumn 访问父值
How to access parent value from nested TableColumn
我有一个 table (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()));
}
我找到的解决方案包括调用 TableColumn::getCellObservableValue
使用与 table 中相应行对应的值作为参数(由回调函数中使用的 CellDataFeatures
对象返回生成一个值)
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);
}
}
我有一个 table (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()));
}
我找到的解决方案包括调用 TableColumn::getCellObservableValue
使用与 table 中相应行对应的值作为参数(由回调函数中使用的 CellDataFeatures
对象返回生成一个值)
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);
}
}