Reputation: 275
I have 2 window application built via SceneBuilder, first one consist of few buttons and tables that list bunch of users, second one should print out details about selected user.
They should communicate so that clicking on lines in the first window should change values in the second, and tweaking values in the second should modify visible fields in the first window(but for now I'm just trying to make the first part work).
I've got tableView working and detecting selection events when clicking on tableView row, and fields in second window initialize to initial user values(I'm tracking these via CustomerEmiter that holds its own customer field). But they aren't changing afterwards even though CustomerEmiter fields are being modified.
Window 1 controller 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));
});
}
Window 2 controller snipet:
@FXML
private TextField userNameTextField;
@Override
public void initialize(URL location, ResourceBundle resources) {
Bindings.bindBidirectional(userNameTextField.textProperty(),
CustomerEmiter.getCustomerEmiter().getCustomer()
.userNameProperty());
}
CustomerEmiter class:
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;
}
}
Customer Class:
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;
}
Upvotes: 0
Views: 1044
Reputation: 275
I've solved the problem by merging both .fxml files into single file and getting rid of bindings. I opted for using
textProperty().setValue
on table row selection event instead.
Upvotes: 1