JavaFX tableview启用和禁用行选择

时间:2017-04-13 11:43:29

标签: javafx tableview

作为我项目的一部分,我希望显示一个表视图,该视图应该被禁用一段时间(在编辑时nb:not table editting)。因此,我得到了一个可以禁用tableview的工作代码。这是工作代码,

table.setSelectionModel(null);

所以我的问题是在编辑过程结束后点击按钮我想要启用它,但不幸的是我无法找到任何替代代码。任何一个请建议我的代码将启用该行选择。任何答案都会很明显。

2 个答案:

答案 0 :(得分:1)

您可以在创建表时检索默认选择模型:

TableView<T> table = new TableView<>();
TableViewSelectionModel<T> defaultSelectionModel = table.getSelectionModel();

其中T是表格的类型。 (当然,如果您使用FXML,只需将第二行放在控制器的initialize()方法中。)

然后禁用行选择

table.setSelectionModel(null);

并再次启用

table.setSelectionModel(defaultSelectionModel);

这是一个SSCCE:

import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableView.TableViewSelectionModel;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TableWithDisabledSelection extends Application {

    @Override
    public void start(Stage primaryStage) {

        TableView<Person> table = new TableView<>();
        TableViewSelectionModel<Person> defaultSelectionModel = table.getSelectionModel();

        table.getColumns().add(column("First Name", Person::firstNameProperty));
        table.getColumns().add(column("Last Name", Person::lastNameProperty));
        table.getColumns().add(column("Email", Person::emailProperty));

        table.getItems().addAll(
                new Person("Jacob", "Smith", "jacob.smith@example.com"),
                new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                new Person("Ethan", "Williams", "ethan.williams@example.com"),
                new Person("Emma", "Jones", "emma.jones@example.com"),
                new Person("Michael", "Brown", "michael.brown@example.com") 
        );

        CheckBox enableSelection = new CheckBox("Enable selection");
        enableSelection.setSelected(true);
        enableSelection.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {

            if (isNowSelected) {
                table.setSelectionModel(defaultSelectionModel);
            } else {
                table.setSelectionModel(null);
            }


        });

        BorderPane root = new BorderPane(table);
        BorderPane.setAlignment(enableSelection, Pos.CENTER);
        BorderPane.setMargin(enableSelection, new Insets(5));
        root.setBottom(enableSelection);

        primaryStage.setScene(new Scene(root, 600, 600));
        primaryStage.show();
    }

    private <S,T> TableColumn<S,T> column(String title, Function<S,Property<T>> prop) {
        TableColumn<S,T> col = new TableColumn<>(title);
        col.setCellValueFactory(cellData -> prop.apply(cellData.getValue()));
        return col ;
    }

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

    public static class Person {

        private final StringProperty firstName = new SimpleStringProperty();
        private final StringProperty lastName = new SimpleStringProperty();
        private final StringProperty email = new SimpleStringProperty();

        public Person(String firstName, String lastName, String email) {
            setFirstName(firstName);
            setLastName(lastName);
            setEmail(email);
        }

        public final StringProperty firstNameProperty() {
            return this.firstName;
        }


        public final String getFirstName() {
            return this.firstNameProperty().get();
        }


        public final void setFirstName(final String firstName) {
            this.firstNameProperty().set(firstName);
        }


        public final StringProperty lastNameProperty() {
            return this.lastName;
        }


        public final String getLastName() {
            return this.lastNameProperty().get();
        }


        public final void setLastName(final String lastName) {
            this.lastNameProperty().set(lastName);
        }


        public final StringProperty emailProperty() {
            return this.email;
        }


        public final String getEmail() {
            return this.emailProperty().get();
        }


        public final void setEmail(final String email) {
            this.emailProperty().set(email);
        }

    }
}

答案 1 :(得分:0)

我用这样的rowfactory来处理这种情况;

tableView.setRowFactory(param -> new TableRow<Model>()
{
    @Override
    protected void updateItem(Model item, boolean empty)
    {
       super.updateItem(item, empty);
       if (!empty)
       {
           disableProperty().bind(item.getFocusable().not());
       }
    }
});

因此,您可以使用tableview模型的适当属性绑定disableproperty。