Anvar
Anvar

Reputation: 434

JavaFX ListView & ContextMenu - getSelectedItem() returns null

I want to use a context menu item on the lines of a listView. In the event handler of the listView's MOUSE_CLICKED event, the getSelectionModel().getSelectedItem() returns the selected item, thats ok. But, when I handle the contextMenuItem's onAction event, it returns null. However, graphically the item is selected. Is there a way to "keep" the selection after the first event handling?

Here is the relevant part of the code:

    ListView<Text> nameList = new ListView<>();
    final ContextMenu cCm = new ContextMenu();
    MenuItem cItem = new MenuItem("someText");
    cCm.getItems().add(cItem);

...

nameList.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent e) {
            if (e.getButton() == MouseButton.SECONDARY) {
                    //its OK here:
                    System.out.println(nameList.getSelectionModel().getSelectedItem().getText());
                    cCm.show(nameList, e.getScreenX(), e.getScreenY());
            }
        }
    });

    cItem.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent e) {
            final Stage dialog = new Stage();
            dialog.initModality(Modality.WINDOW_MODAL);
            //nullPointerException on the following:
            Text t = new Text(nameList.getSelectionModel().getSelectedItem().getText());
            //showing dialog, etc.

Upvotes: 1

Views: 5770

Answers (1)

j will
j will

Reputation: 3807

I pretty much created an exact replica of what you did, and my implementation worked:

private void initRandomCardListView() {
    populateRandomList();
    final ContextMenu randomListContextMenu = new ContextMenu();
    MenuItem replaceCardMenuItem = new MenuItem("Replace");
    replaceCardMenuItem.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            replaceRandomCard();
        }
    });
    randomListContextMenu.getItems().add(replaceCardMenuItem);

    randomCardList.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            if (event.getButton().equals(MouseButton.SECONDARY)) {
                randomListContextMenu.show(randomCardList, event.getScreenX(), event.getScreenY());
            }
        }
    });
}

private void replaceRandomCard() {
    System.out.println("jobs done");
    System.out.println("card selected: " + randomCardList.selectionModelProperty().get().getSelectedItem().toString());
    System.out.println("card index: " + randomCardList.getSelectionModel().getSelectedIndex());
    System.out.println("card index: " + randomCardList.getSelectionModel().getSelectedItem().toString());
}

I don't have any null pointer exceptions. Overall your implementation looks good. There is most likely something that is wrong with the items in your listview.

Upvotes: 5

Related Questions