JavaFX TableView-TextField绑定

时间:2015-04-30 12:23:37

标签: java javafx-8

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

他们应该沟通,以便点击第一个窗口中的行应该更改第二个窗口中的值,而调整第二个窗口中的值应该修改第一个窗口中的可见字段(但是现在我只是想尝试制作第一个部分工作)。

我有tableView工作并在点击tableView行时检测选择事件,第二个窗口中的字段初始化为初始用户值(我通过拥有自己的客户字段的CustomerEmiter跟踪这些值)。但即使正在修改CustomerEmiter字段,它们也不会随后发生变化。

Window 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));
    });
}

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

1 个答案:

答案 0 :(得分:1)

我通过将两个.fxml文件合并到单个文件中并删除绑定来解决问题。我选择使用

textProperty().setValue

表格行选择事件。