在上下文菜单中过滤节点的事件?

时间:2015-01-01 23:50:17

标签: java javafx javafx-8

我在TableView中有一个上下文菜单,其中包含一些要与之交互的高级控件。这有点烦人,因为当您在上下文菜单中单击控件时,它会执行该事件,但它也会触发使上下文菜单消失的事件。

我究竟如何为该控件中的上下文菜单实现过滤器?

Pane filterPane = buildFilterPane();

CustomMenuItem menuItem = new CustomMenuItem();

menuItem.contentProperty().set(filterPane);
// I want all click events inside the filterPane to only be heard by the filterPane

2 个答案:

答案 0 :(得分:0)

我认为你在寻找

menuItem.setHideOnClick(false);

答案 1 :(得分:0)

这是James_D解决方案的一个例子。我刚创建它是因为我很想知道如何在上下文菜单保持打开状态时通过选择上下文菜单中的列来更改表列的可见性,以便您可以选择多个列。

public class TableWithContextMenu extends Application {

    private TableView<Person> table = new TableView<Person>();
    private final ObservableList<Person> data =
            FXCollections.observableArrayList( 
            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"));
    final HBox hb = new HBox();

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

    @Override
    public void start(Stage stage) {

        Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(450);
        stage.setHeight(550);

        TableColumn<Person, String> firstNameCol = new TableColumn<Person, String>("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));

        TableColumn<Person, String> lastNameCol = new TableColumn<Person, String>("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));

        TableColumn<Person, String> emailCol = new TableColumn<Person, String>("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));

        table.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

        // build context menu 
        // --------------------------------
        ContextMenu contextMenu = new ContextMenu();

        MenuItem item1 = new MenuItem("Closing Item");
        item1.setOnAction(e -> System.out.println("Clicked: " + e));

        CustomMenuItem item2 = new CustomMenuItem(new Label("Stay Open Item"));
        item2.setHideOnClick(false);
        item2.setOnAction(e -> System.out.println("Clicked: " + e));

        CustomMenuItem item3 = new CustomMenuItem(new Label("Manually Closing Item"));
        item3.setHideOnClick(false);
        item3.setOnAction(e -> {
            System.out.println("Clicked: " + e);
            contextMenu.hide();
        });

        CheckBox firstNameCheckBox = new CheckBox("First Name");
        firstNameCheckBox.selectedProperty().bindBidirectional( firstNameCol.visibleProperty());
        CustomMenuItem item4 = new CustomMenuItem( firstNameCheckBox);
        item4.setHideOnClick(false);

        CheckBox lastNameCheckBox = new CheckBox("Last Name");
        lastNameCheckBox.selectedProperty().bindBidirectional( lastNameCol.visibleProperty());
        CustomMenuItem item5 = new CustomMenuItem( lastNameCheckBox);
        item5.setHideOnClick(false);

        CheckBox emailCheckBox = new CheckBox("email");
        emailCheckBox.selectedProperty().bindBidirectional( emailCol.visibleProperty());
        CustomMenuItem item6 = new CustomMenuItem( emailCheckBox);
        item6.setHideOnClick(false);

        CustomMenuItem item7 = new CustomMenuItem(new CheckBox("Custom Action"));
        item7.setHideOnClick(false);
        item7.setOnAction(e -> {System.out.println( e);});

        contextMenu.getItems().addAll(item1, item2, item3, new SeparatorMenuItem(), item4, item5, item6, new SeparatorMenuItem(), item7);

        // set context menu
        table.setContextMenu(contextMenu);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll( table);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
        private final SimpleStringProperty email;

        private Person(String fName, String lName, String email) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String fName) {
            email.set(fName);
        }
    }
} 

enter image description here