监听JavaFX中是否禁用或启用了按钮

时间:2014-11-12 21:54:47

标签: javafx

我想要一个代码,允许我监听TextField组件上的任何更改,以禁用或启用名为save的按钮。是否可以使用绑定或类似的东西来做到这一点?

例如:save.disableProperty().bind(...) 我使用了两个TextField组件来更改TableView中存储的信息 我使用了数据绑定,以便在选择TableView行时,该行中的信息显示在TextField中。

@FXML
private TableView<UserData> table;
@FXML
private TableColumn<UserData, String> col1;
@FXML
private TableColumn<UserData, String> col2;
@FXML
private TextField name;
@FXML
private TextField email;

@FXML
Button newUser;
@FXML
Button save;
@FXML
Button delete;
private Connection conn;
private Statement stm;
private PreparedStatement prep;
private ResultSet result;

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    populateTableView();
    configureDisable();
    table.getSelectionModel().selectedItemProperty().addListener(userDataListener);
    name.disableProperty().bind(table.getSelectionModel().selectedItemProperty().isNull());
    email.disableProperty().bind(table.getSelectionModel().selectedItemProperty().isNull());
    delete.disableProperty().bind(table.getSelectionModel().selectedItemProperty().isNull());
    data.addListener((Change<? extends UserData> change) -> save.setDisable(false));
    name.setOnAction(ev -> email.requestFocus());
}

private final ChangeListener<UserData> userDataListener = ((v, oldV, newV) -> {
    if (oldV != null) {
        name.textProperty().unbindBidirectional(oldV.nameProperty());
        email.textProperty().unbindBidirectional(oldV.emailProperty());

    }
    if (newV != null) {
        name.textProperty().bindBidirectional(newV.nameProperty());
        email.textProperty().bindBidirectional(newV.emailProperty());

    }
    private void populateTableView() {
        try {
            conn = CreateConnection.getConnection();
            stm = conn.createStatement();
            data = FXCollections.observableArrayList();
            result = conn.createStatement().executeQuery("select *from user");
            while (result.next()) {
                data.add(new UserData(result.getString("Name"), result.getString("Email")));
            }
            col1.setCellValueFactory(new PropertyValueFactory<UserData, String>("name"));
            col2.setCellValueFactory(new PropertyValueFactory<UserData, String>("email"));
            table.setItems(data);
        } catch (IOException ex) {
            Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
});

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您正在使用文本字段更改表格中的数据(特别是nameemail属性),并且当表格中的这些值发生变化时,您需要启用保存按钮。

您可能需要侦听器(而不是绑定)才能执行此操作。一种方法是从具有extractor的表项创建ObservableList以提取感兴趣的属性。当列表更改(包括来自任何列表元素的那些属性)时,将保存按钮的disable属性设置为false。

类似的东西:

    ObservableList<UserData> observableData = FXCollections.observableList(table.getItems(), 
            user -> new Observable[] {user.nameProperty(),  user.emailProperty()});
    observableData.addListener((Change<? extends Person> change) -> save.setDisable(false));