【发布时间】:2015-09-02 09:37:53
【问题描述】:
我的用户界面如下所示:
我想要的是,当您单击表格中的 1 时,文本框在表格上显示 2 和 3,它显示 4,并且当您在文本框中键入时,所选行的对象上的属性一定会更新。
所以我有一个具有两个属性的对象集合(value1 - 在表中,value2 在文本框中)。
现在是复杂性:我想通过双向绑定而不是事件侦听器来做到这一点。
基本上文本框的绑定可以这样描述:
将 text 属性双向绑定到 table view 的 selected item 属性的 value2 属性
这有可能吗?
我一直在摆弄,我得到了这个,但它当然不是双向的(我希望可能必须提供第二个回调才能朝相反的方向前进):
this.textField.textProperty().bind(
Bindings.createStringBinding(() -> {
if (this.tableView.getSelectionModel().getSelectedItem() == null) {
return "";
}
return String.valueOf(this.tableView.getSelectionModel().getSelectedItem().value2Property().get());
},
this.tableView.getSelectionModel().selectedItemProperty()));
这是我正在谈论的完整工作示例,如果它更清楚的话:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public final class Program extends Application {
private static final class SomeSortOfController implements Initializable {
@FXML private TableView<SomeSortOfObject> tableView;
@FXML private TableColumn<SomeSortOfObject, Number> value1TableColumn;
@FXML private TextField textField;
@Override
public void initialize(final URL url, final ResourceBundle resourceBundle) {
this.tableView.setItems(FXCollections.observableArrayList(
new SomeSortOfObject(1,2),
new SomeSortOfObject(3,4)));
this.value1TableColumn.setCellValueFactory(c -> c.getValue().value1Property());
this.value1TableColumn.setEditable(false);
// Here I need to come up with some sort of binding from the selected item
// in the table view to the value2 property in the SomeSortOfObject (and
// vice versa)
}
}
private static final class SomeSortOfObject {
private final IntegerProperty value1;
private final IntegerProperty value2;
public SomeSortOfObject(int value1, int value2) {
this.value1 = new SimpleIntegerProperty(value1);
this.value2 = new SimpleIntegerProperty(value2);
}
public IntegerProperty value1Property() {
return this.value1;
}
public IntegerProperty value2Property() {
return this.value2;
}
}
public static void main(final String[] args) {
launch(args);
}
@Override
public void start(final Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/main.fxml"));
loader.setController(new SomeSortOfController());
stage.setScene(new Scene(loader.load(), 200, 100));
stage.show();
}
}
为了完整,这是 main.fxml 文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<HBox xmlns="http://javafx.com/javafx/8.0.45" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TableView fx:id="tableView">
<columns>
<TableColumn text="value1" fx:id="value1TableColumn" />
</columns>
</TableView>
<TextField fx:id="textField" />
</children>
</HBox>
【问题讨论】:
-
你不能 - selectedItem/Index 是只读的
-
@kleopatra,谢谢,但我不想在表格视图上设置 selectedItem/Index - 我想在我的对象上设置 value2Property 和文本框的 text 属性
-
我觉得我的标题很差
-
hmm .. 但是,问题出在哪里? You can replace the binding whenever the selection changes.我可能仍然误解你的问题:-)
-
您可以执行一次,然后在需要时重复使用。有几种实现,我非常简单的一个叫做PathAdapter :-)
标签: java binding javafx javafx-8