【问题标题】:Update Buttons JavaFX when a selected process in TableView ends当 TableView 中的选定进程结束时更新按钮 JavaFX
【发布时间】:2016-07-28 10:40:40
【问题描述】:

我想在选定行中的进程结束时禁用/启用 BorderPane 下的按钮。

我试试这个

downloadTable.getSelectionModel().getSelectedIndices().addListener(new ListChangeListener<Integer>() {
        @Override
        public void onChanged(Change<? extends Integer> c) {
            int selectedIndex = downloadTable.getSelectionModel().getSelectedIndex();
            if (downloadTable.getItems().get(selectedIndex).getStatus() == Download.DOWNLOADING) {
                cancelButton.setDisable(false);
            } else {
                cancelButton.setDisable(true);
            }

        }
    });

但它仅在我切换到已结束的项目(下载)时才有效。 我想要做的是在选择项目时启用/禁用按钮。 谢谢大家

example of ended download with cancelButton that I want to disable

【问题讨论】:

  • 你能显示表格的模型类吗?

标签: button javafx tableview refresh listener


【解决方案1】:

也许这样的事情可以帮助你:

public class Main {

    private Button someButton;
    private TableView<?> downloadTable;

    private void someMethod() {
        //somecode
        Callback<TableView<?>, TableRow<?>> baseFactory = downloadTable.getRowFactory();
        downloadTable.setRowFactory( new CustomRowFactory<?>( someButton, baseFactory ) );
        //somecode
    }

}

public class CustomRowFactory<T> implements Callback<TableView<T>, TableRow<T>> {

    private final Callback<TableView<T>, TableRow<T>> baseFactory;
    private final Button someButton;

    public CustromRowFactory( Button someButton, Callback<TableView<T>, TableRow<T>> baseFactory) {
        this.someButton = somButton;
        this.baseFactory = baseFactory;
    }

    @Override
    public TableRow<T> call(TableView<T> tableView) {
        final TableRow<T> row = baseFactory == null ? row = new TableRow<>() : row = baseFactory.call( tableView );
        someButton.disableProperty().bind(
            row.selectedProperty().and( row.getItem().statusProperty().isNotEquals(Download.DOWNLOADING) )
        );
        return row;
    }

}

或在您的某些TableCell 实现中插入绑定。

【讨论】:

    猜你喜欢
    • 2012-12-02
    • 1970-01-01
    • 2014-07-28
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 2013-05-19
    相关资源
    最近更新 更多