我有2个通过SceneBuilder构建的窗口应用程序,第一个窗口由几个按钮和表组成,这些列表列出了一群用户,第二个窗口应打印出有关所选用户的详细信息。

他们应该进行沟通,以便在第一个窗口中单击行应更改第二个窗口中的值,并在第二个窗口中调整值应修改第一个窗口中的可见字段(但现在我只是想使第一部分起作用)。

单击tableView行时,我可以使用tableView并检测选择事件,并且第二个窗口中的字段初始化为初始用户值(我通过拥有自己的customer字段的CustomerEmiter跟踪这些值)。但是即使修改了CustomerEmiter字段,它们之后也不会更改。

窗口1控制器片段:

@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控制器片段:

@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;
    }

最佳答案

我已经通过将两个.fxml文件合并为单个文件并摆脱了绑定来解决了这个问题。我选择使用

textProperty().setValue


在表格行选择事件上。

07-24 15:48