Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger event when double click on a tree node

I have this code which creates new tab in a remote Java Class.

treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<String>>()
        {
            @Override
            public void changed(ObservableValue<? extends TreeItem<String>> observable, TreeItem<String> oldValue, TreeItem<String> newValue)
            {
                System.out.println("Selected Text : " + newValue.getValue());
                // Create New Tab
                Tab tabdata = new Tab();
                Label tabALabel = new Label("Test");
                tabdata.setGraphic(tabALabel);

                DataStage.addNewTab(tabdata);
            }
        });

Can you tell me how I can modify the code to open new tab when I double click on a tree node. In my code the tab is opened when I click once. What event handler do I need?

like image 562
Peter Penzov Avatar asked Jun 27 '13 16:06

Peter Penzov


1 Answers

You can add an EventHandler<MouseEvent> to the TreeView.setOnMouseClicked() method and check for the getClickCount() return value of the MouseEvent to determine if it was a double click. Remove the ChangeListener above and add the logic to the EventHandler.

Use the description here and apply it to your treeView variable.

It'll look something like this. You'll probably want to check the item for null as well.

treeView.setOnMouseClicked(new EventHandler<MouseEvent>()
{
    @Override
    public void handle(MouseEvent mouseEvent)
    {            
        if(mouseEvent.getClickCount() == 2)
        {
            TreeItem<String> item = treeView.getSelectionModel().getSelectedItem();
            System.out.println("Selected Text : " + item.getValue());

            // Create New Tab
            Tab tabdata = new Tab();
            Label tabALabel = new Label("Test");
            tabdata.setGraphic(tabALabel);

            DataStage.addNewTab(tabdata);
        }
    }
});
like image 79
OttPrime Avatar answered Oct 18 '22 08:10

OttPrime