0
我有2個窗口應用程序通過SceneBuilder構建,第一個包含幾個按鈕和表列出一堆用戶,第二個應打印出有關選定用戶的詳細信息。JavaFX TableView-TextField綁定
他們應該進行通信,以便點擊第一個窗口中的行應該在第二個窗口中更改值,並且在第二個窗口中調整值應該修改第一個窗口中的可見字段(但現在我只是想讓第一個窗口部分工作)。
我有tableView工作和檢測選擇事件時點擊tableView行,並在第二個窗口中的字段初始化爲初始用戶值(我通過CustomerEmiter持有自己的客戶字段跟蹤這些)。但即使CustomerEmiter字段被修改,它們之後也不會改變。
窗口1個控制器snipet:
@Override
public void initialize(URL location, ResourceBundle resources) {
id.setCellValueFactory(new PropertyValueFactory<>("id"));
userName.setCellValueFactory(new PropertyValueFactory<>("userName"));
// grab users from the DB
getUsers();
customerTableView.setItems(customersObservableList);
customerTableView.getSelectionModel().selectedItemProperty()
.addListener((ObservableValue<? extends Customer> observable, Customer oldValue, Customer newValue) -> {
if (observable != null && observable.getValue() != null) {
CustomerEmiter.getCustomerEmiter()
.getCustomer().setUserName(newValue.userNameProperty());
}
});
}
private ObservableList<Customer> customersObservableList = FXCollections.observableArrayList();
public void getUsers() {
List<CustomerObject> customerList = DataTransferObject.getCustomers();
customerList.stream().forEach((customerO) -> {
customersObservableList.add(new Customer(customerO));
});
}
窗口2控制器snipet:
@FXML
private TextField userNameTextField;
@Override
public void initialize(URL location, ResourceBundle resources) {
Bindings.bindBidirectional(userNameTextField.textProperty(),
CustomerEmiter.getCustomerEmiter().getCustomer()
.userNameProperty());
}
CustomerEmiter類:
public class CustomerEmiter {
private static CustomerEmiter instance = null;
private static Customer customer = new Customer(new CustomerObject());
protected CustomerEmiter() {
}
public void setCustomer(Customer customer) {
CustomerEmiter.customer = customer;
}
public Customer getCustomer() {
return customer;
}
public static CustomerEmiter getCustomerEmiter() {
if(instance == null) {
instance = new CustomerEmiter();
}
return instance;
}
}
客戶類別:
public class Customer {
private IntegerProperty id;
private StringProperty userName;
public Customer(CustomerObject customer) {
this.id = new SimpleIntegerProperty(customer.id);
this.userName = new SimpleStringProperty(customer.userName);
public int getId() {
return this.id.get();
}
public String getUserName() {
if(this.userName.isNull().get()) {
return "empty";
}
return userName.get();
}
public StringProperty userNameProperty() {
if(this.userName.isNull().get()) {
return new StringProperty("empty");
}
return userName;
}
你能提供客戶分類的片段嗎?你的代碼對我來說似乎很好。我希望看到getUsers()以及'customersObservableList'是如何初始化的 –
向window1控制器和Customer類添加了相關信息,我在newUser/deleteUser按鈕上重新填充了ObservableList,因此我將它們排除了。我已經刪除了數據庫連接直到我知道了,所以DataTransferObject.getCustomers()現在只創建5個虛擬客戶。 – user3533671
好的,你是否嘗試過調試?你可以粘貼stackTrace嗎? –