JavaFX:如何禁用 TableView 中的行?

标签 javafx tableview javafx-8 tablerow

我想禁用 TableView 中的一行。我有一个产品的 Tableview,并且我已经知道需要禁用哪个产品(我从填充 TableView 的 ObservableList 中获取了它的索引)。

如何获取与 ObservableList 中的 Product 关联的 TableRow,并且我知道该 TableRow 的索引?

否则:有没有一种简单的方法可以从 TableView 中禁用特定的 TableRow?

非常感谢任何帮助。

最佳答案

最好的方法不是使用索引,而是使用自定义行工厂并观察行中项目的适当属性。

这对于当前的 API 来说有点棘手,因为您可能需要观察表行的 item 属性的属性。您可以使用Bindings.select(...)这样做,但是当该项目为空时,当前版本会发出许多多余的警告(这种情况会很频繁)。我更喜欢使用EasyBind framework对于这种功能。

此示例禁用所显示项目的 value 属性小于 5 的所有表行:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import org.fxmisc.easybind.EasyBind;

public class DisabledTableRowExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Item> table = new TableView<>();
        table.getItems().addAll(createData());

        TableColumn<Item, Item> deleteCol = createTableColumn("Delete", ReadOnlyObjectWrapper<Item>::new);
        deleteCol.setCellFactory(this::createDeleteCell);

        table.getColumns().addAll(Arrays.asList(
                createTableColumn("Name", Item::nameProperty),
                createTableColumn("Value", Item::valueProperty),
                deleteCol 
        ));

        // A row factory that returns a row that disables itself whenever the
        // item it displays has a value less than 5:

        table.setRowFactory(tv -> {
            TableRow<Item> row = new TableRow<>();

            // use EasyBind to access the valueProperty of the itemProperty of the cell:
            row.disableProperty().bind(
                    EasyBind.select(row.itemProperty()) // start at itemProperty of row
                    .selectObject(Item::valueProperty)  // map to valueProperty of item, if item non-null
                    .map(x -> x.intValue() < 5) // map to BooleanBinding via intValue of value < 5
                    .orElse(false)); // value to use if item was null

            // it's also possible to do this with the standard API, but there are lots of 
            // superfluous warnings sent to standard out:
            // row.disableProperty().bind(
            //          Bindings.selectInteger(row.itemProperty(), "value")
            //          .lessThan(5));

            return row ;
        });
        BorderPane root = new BorderPane(table);
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private List<Item> createData() {
        Random rng = new Random();
        List<Item> data = new ArrayList<>();
        for (int i=1; i<=20; i++) {
            data.add(new Item("Item "+i, rng.nextInt(10)));
        }
        return data ;
    }

    private <S,T> TableColumn<S, T> createTableColumn(String name, Function<S, ObservableValue<T>> propertyMapper) {
        TableColumn<S,T> col = new TableColumn<>(name);
        col.setCellValueFactory(cellData -> propertyMapper.apply(cellData.getValue()));
        return col ;
    }

    private TableCell<Item, Item> createDeleteCell(TableColumn<Item, Item> col) {
        ObservableList<Item> itemList = col.getTableView().getItems();
        TableCell<Item, Item> cell = new TableCell<>();
        cell.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        Button button = new Button("Delete");
        button.setOnAction(event -> itemList.remove(cell.getItem()));
        cell.graphicProperty().bind(Bindings.when(cell.emptyProperty()).then((Node)null).otherwise(button));
        return cell ;
    }

    public static class Item {
        private final StringProperty name = new SimpleStringProperty(this, "name");
        private final IntegerProperty value = new SimpleIntegerProperty(this, "value");
        public final StringProperty nameProperty() {
            return this.name;
        }
        public final java.lang.String getName() {
            return this.nameProperty().get();
        }
        public final void setName(final java.lang.String name) {
            this.nameProperty().set(name);
        }
        public final IntegerProperty valueProperty() {
            return this.value;
        }
        public final int getValue() {
            return this.valueProperty().get();
        }
        public final void setValue(final int value) {
            this.valueProperty().set(value);
        }

        public Item(String name, int value) {
            setName(name);
            setValue(value);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

如果您确实根据索引进行禁用,则可以使用非常相似的技术:

IntegerProperty disabledRowIndex = new SimpleIntegerProperty();
// ...
// in row factory do:
row.disableProperty().bind(row.indexProperty.isEqualTo(disabledRowIndex));

然后调用 disabledRowIndex.set(...) 将禁用所提供索引处的行。

请注意,disable 语义可能不完全是您想要的。这会禁用表行的所有输入(例如,删除按钮将不会启用);但是它不会阻止选择行(键盘导航由 TableView 本身管理,因此您仍然可以使用键盘选择行)。定义自定义选择行为更具挑战性。

关于JavaFX:如何禁用 TableView 中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26607144/

相关文章:

JAVA FX - 隐藏阶段丢失的服务任务响应

Gridpane 中的 JavaFx 图像显着降低性能

objective-c - 如何在我的自定义 TableviewCell 中访问 ImageView 而无需创建其导出

JavaFX 8 : Intercepting appication "exit"

Java FX 8,无法设置文本字段的值

java - 在 JavaFX FXML 中启动最大化的应用程序窗口无法正常工作

Javafx setImage for ImageView 来自另一个 FXML Controller

JavaFX 表格 View 不显示按钮

ios - 在 Swift 4 的 UITableView 的 didSelectRowAt 中指定单元格选择范围

java - 如何绑定(bind)反向 boolean 值,JavaFX