Disable SpreadsheetView cell selection via mouse/key events, but retain ability to scroll the view

435 Views Asked by At

I'm writing an Excel import wizard which loads a user's spreadsheet into a ControlsFX SpreadsheetView. The user can then identify the relevant columns (i.e., first and last name, etc.) to be imported into the app via a set of combo boxes. Each combo box contains a list of the available columns. When the user makes a choice in a combo box, the respective column is added to the selection model's range of selections.

Column selection should only be done via the combo boxes, and user clicks on cells in the spreadsheet view should be ignored (i.e, not change the selection model contents).

However, I cannot find a way to disable mouse clicks on cells or column headers. The solution to a related question about how to disable clicks on TableView doesn't provide a satisfactory answer: setMouseTransparent(true) essentially disables the entire view.

In my case, the user must be able to scroll the view in order to find relevant columns, since not all users of the app have structured their spreadsheet in the same column order and the column may not be visible without scrolling horizontally.

So to re-iterate: I need to consume mouse clicks on cells and column headers to prevent changing the column selections made using the combo boxes. How do I do this? (FYI: consuming mouse events on the spreadsheet view [setOnMousePressed|Released|Clicked(mEvent -> mEvent.consume()] has no effect.)

1

There are 1 best solutions below

1
On BEST ANSWER

Actually, when you use the setOnMousePressed methods, you are adding an EventHandler. EventHandler are happening in the later chain of bubbling event.

What you want is to add an EventFilter that will catch the event before the SpreadsheetView has it.

For example, I've made this :

spreadSheetView.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                event.consume();
            }
        });
        spreadSheetView.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                event.consume();
            }
        });
        spreadSheetView.addEventFilter(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                event.consume();
            }
        });

And it's working. Although I really don't know the side effect of such a manipulation..