2015-01-01 61 views
0

我有一個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 

回答

0

我認爲你正在尋找

menuItem.setHideOnClick(false); 
+0

按你的建議只是嘗試。它仍然沒有工作。明天我會安排一個SSCCE。 – tmn

+0

James_D的解決方案確實有效。那麼究竟哪些不適合你?請提供完整的示例代碼。 – Roland

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", "[email protected]"), 
      new Person("Isabella", "Johnson", "[email protected]"), 
      new Person("Ethan", "Williams", "[email protected]"), 
      new Person("Emma", "Jones", "[email protected]"), 
      new Person("Michael", "Brown", "[email protected]")); 
    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

+0

這很有趣。我點擊了「保持打開的項目」,它的工作原理和預期的行爲,但是這些複選框會在點擊後關閉整個contextMenu,即使它們的setHideOnClick()設置爲false。 – tmn

+0

這很奇怪。這個對我有用。你使用哪個Java版本?我使用1.8.0_25。我希望你不要用可能影響行爲的參數來啓動應用程序。 – Roland

+0

它看起來像我的Eclipse正在使用JRE的1.8.0_20,並且我正在使用Windows 8.1。也許我應該升級,我會讓你知道。 – tmn

相關問題