2014-11-06 55 views
0

我的问题是: 我做了一个组合框,我想在它的元素上使用上下文菜单,所以当我设置如下所示的cellfactory时,我看不到任何更多的项目并且上下文菜单不显示。javafx combobox contextmenu not diplayed

CBXGroups.setCellFactory(new Callback<ListView<String>, ListCell<String>>() { 
    public ListCell<String> call(ListView<String> param) { 
    final ListCell<String> cell = new ListCell<String>(); 
    final ContextMenu cellMenu = new ContextMenu(); 
    MenuItem rimuoviDalControllo = new MenuItem("RIMUOVI DAL CONTROLLO"); 
    MenuItem rimuoviDefinitivamente = new MenuItem("RIMUOVI DEFINITIVAMENTE"); 
    rimuoviDalControllo.setOnAction(new EventHandler<ActionEvent>() { 
     public void handle(ActionEvent event) { 
     Service.deleteGroupFromControl(cell.getText(),CBXControllo.getSelectionModel().getSelectedItem()); 
     populateLists(); 
     } 
    }); 
    rimuoviDefinitivamente.setOnAction(new EventHandler<ActionEvent>() { 
     public void handle(ActionEvent event) { 
     Service.deleteGroup(cell.getText()); 
     populateLists(); 
     } 
    }); 
    cellMenu.setOnShowing(new EventHandler<WindowEvent>() { 
     public void handle(WindowEvent event) { 
     cell.requestFocus(); 
     } 
    }); 
    cellMenu.getItems().addAll(rimuoviDalControllo,rimuoviDefinitivamente); 
    cell.contextMenuProperty().bind(Bindings.when(Bindings.isNotNull(cell.itemProperty())).then(cellMenu).otherwise((ContextMenu) null)); 
    return cell; 
    } 
}); 

回答

0

由于未在ListCell中设置文字,因此看不到项目。你可以用一行代码来做到这一点:

cell.textProperty().bind(cell.itemProperty()); 

上下文菜单更棘手,我没有真正的解决方案。问题是ComboBox使用PopupControl显示列表视图,并且弹出控件的autoHide设置为true。所以当你点击列表视图时,弹出关闭(阻止你看到上下文菜单)。没有办法访问弹出控件,所以我不认为会有任何方式来做到这一点。

在组合框中注册一个上下文菜单似乎是一件不寻常的事情;我想知道你想做什么有更好的方法。 A MenuButton在某些方面类似于ComboBox(显示带选项的弹出窗口的控件),但它具有菜单层次结构,因此可以包含级联菜单。这可能会提供您想要的功能。