-2

I would like to run a function when a Tab is clicked. I tried to do this in my fxml but I think Tabs do not have an onAction property to call.

<Tab fx:id="Tab1" text="Tab1" onAction="#loadTab1"> 

</Tab>

So I get this error when I run the application:

Caused by: java.lang.UnsupportedOperationException: Cannot determine type for property.
Ricky
  • 553
  • 6
  • 21
  • You want something to happen when you *click* but listening for a *click* doesn't sound like a good idea to you? ;) If you need help with your code, please include your code. [Edit] your question and add a [mcve]. – Zephyr Aug 06 '19 at 22:43
  • Maybe I was not clear enough. But as I pointed out in my question I would like to make a function call, referenced in my fxml. Using an onAction could have been a good idea, but Tab do not support that. So I just expected that there is an onClick, onMouseCliked or something similar to that property instead of onAction. But I do not feel the need to provide a reproducible example. I think it's enough without that. – Ricky Aug 06 '19 at 23:26
  • `Tab` does not provide mouse events either. It's just a class containing data necessary for `TabPane` to determine how to set up it's descendants to properly display the tab. There is a `onSelectionChanged` property though. Listening to the `selectedItem` property of the `TabPane`s selection model may be preferrable though... – fabian Aug 06 '19 at 23:29
  • You are right fabian. That is the only solution I could find so far. With that I know when the tab is selected and deselected combining with the `isSelected()` function. Thank you guys for the help. – Ricky Aug 06 '19 at 23:35

1 Answers1

0

One simple solution to register the tab selection is this:

Tab1.setOnSelectionChanged(e -> {
            System.out.println(Tab1.isSelected() ? "Tab1 is selected" : "Tab1 is not selected" );
        });
Ricky
  • 553
  • 6
  • 21