7

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?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

2 Answers2

12

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);
        }
    }
});
Community
  • 1
  • 1
OttPrime
  • 1,878
  • 1
  • 15
  • 24
  • Would you please show me some implementation code? I'm new to JavaFX and this is something advanced for me. – Peter Penzov Jun 28 '13 at 18:05
  • What if they expand a node with another method, like the keyboard? I guess we have to bind multiple events to the same handler =/. Seems like there should be a single handler for that sort of thing. (onNodeActivated or something) – crush Feb 19 '14 at 19:46
  • @crush This answer was specific to the double-click action. If you want to trigger something off of any expand/collapse event, then you'll want to look at the `TreeItem.expandedProperty()`. You can add a `ChangeListener` to it (maybe through a `TreeCell<>` factory) and add your functionality there. Checkout the answer here: [How to get current TreeItem reference which is expanding by user click in JavaFx 2?](http://stackoverflow.com/a/14241151/1625196) – OttPrime Feb 19 '14 at 22:04
2

In my opinion, the best practice is to implement your cell.

public class DoubleClickCellImpl extends TreeCell<String> {
    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        if (item == null || empty) {
            setText(null);
        } else {
            setText(item);
        }
    }

    public DoubleClickCellImpl() {
        super();

        setOnMouseClicked(event -> {
            TreeItem<String> ti = getTreeItem();
            if (ti == null || event.getClickCount() < 2)
                return;

            // do something here.
        });
    }
}
KerlW
  • 331
  • 2
  • 6