1

I have an application where a TableView will be created dynamically based on a database query. This means that my application will have multiple tables with differing columns.

I must display a separate button to show/hide columns. I am familiar with the table menu button triggered with table.setTableMenuButtonVisible(). Unfortunately, I cannot use the actual on-screen built-in button on the UI, although I would like to use it's functionality.

JavaFX TableView showing menu button

I am essentially looking for a table.getTableMenu().show() sort of call. But I can't find where this is a built in method of any sort. Is there a way that I can call this button's action from a UI button of my own design?

Dustin
  • 693
  • 8
  • 20
  • 1
    Have you seen [*Adapt TableView menu button*](https://stackoverflow.com/q/27739833/230513)? – trashgod Jan 30 '19 at 16:42
  • I have. I unfortunately have the limitation of not using `TableView`'s visual button. I need a separate JavaFX Button that does the same thing as the built-in button. I'd also rather stay away from the com.sun packages unless there's no alternative. – Dustin Jan 30 '19 at 16:52
  • 1
    there is no alternative to accessing/extending TableHeaderRow to lay your hands on the default contextMenu - the headerRow itself resides in public skin package (since fx9), the contextMenu is accessible via reflection only. Don't quite understand what you are after, though: _call this button's on action in my application_ and _separate button that does the same thing_ feels a bit like a contradiction? what do you mean by _without having to implement a show/hide method_? Just grab the corner contextMenu and let your button call show on it .. – kleopatra Jan 30 '19 at 17:04
  • please clarify by editing the question - comments are not good for additional details :) – kleopatra Jan 30 '19 at 17:05

1 Answers1

3

Actually, I was wrong in my comment: it is possible to lookup the corner region and trigger its mousePressedHandler without reflection.

The following code snippet opens the corner menu just as if it had been clicked directly (in fx11 at least, and still dirty in relying on the implementation detail that opening is triggered by a mousePressed event):

Button showCorner = new Button("open menu button");
showCorner.addEventHandler(MouseEvent.MOUSE_PRESSED, e -> {
    Node corner = table.lookup(".show-hide-columns-button");
    corner.fireEvent(e);
});
kleopatra
  • 51,061
  • 28
  • 99
  • 211