Question

I'm using this code to implement select listener:

treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<DynamicTreeNodeModel>>()
        {

            @Override
            public void changed(ObservableValue<? extends TreeItem<DynamicTreeNodeModel>> observable, TreeItem<DynamicTreeNodeModel> oldValue, TreeItem<DynamicTreeNodeModel> newValue)
            {
                System.out.println("Selected Text : " + newValue.getValue());
                ........
            }

        }

I want to use the change listener only when I click with left mouse button. How I can do this?

Was it helpful?

Solution

treeView.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent event) {
        if (event.isSecondaryButtonDown()){
            //do right click stuff
            event.consume();
        }
    }
});

If you still want have the selection change this may be the easiest way. I set a flag if the right button is pressed. You can get the selected TreeItem in the same way in the change listener.

package treeselect;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TreeSelect extends Application {

    private boolean isRightClick;

    @Override
    public void start(Stage stage) {
        final Label sel = new Label();
        TreeView treeView = new TreeView(new TreeItem("root"));
        for (int i = 0; i < 10; i++) {
            treeView.getRoot().getChildren().add(new TreeItem("name" + i));
        }
        treeView.setMinWidth(100);

        treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem>() {
            @Override
            public void changed(ObservableValue observable, TreeItem oldValue, TreeItem newValue) {
                if (newValue != null) {
                    if (isRightClick) {
                        //do all right click stuff here
                        //the selected item is newValue
                        sel.setText("Right click on " + newValue.toString());
                        //reset the flag
                        isRightClick = false;
                    } else {
                        //do your regular change stuff here
                        sel.setText("Not Right Click on " + newValue.toString());
                    }
                }
            }
        });

        treeView.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.isSecondaryButtonDown()) {
                    isRightClick = true;
                }
            }
        });

        VBox root = new VBox();
        root.getChildren().addAll(treeView, sel);
        Scene scene = new Scene(root, 400, 300);
        stage.setScene(scene);
        stage.show();
    }
}

OTHER TIPS

For JavaFX 8, you can use event.getButton() == MouseButton.PRIMARY

for JavaFX 2.2, event.isPrimaryButtonDown()

for Java AWT, event.getButton() == MouseButton.BUTTON1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top