How can I do some action when one specific Tab is selected using Javafx
Asked Answered
B

4

19

I'm trying to do something when one tab of my tabPane is clicked, I've tried use Action Event but it doesn't work:

public void tabPressClicked (ActionEvent event){
        comboBoxPresYear.setVisible(true);
        lblPresYear.setVisible(true);
    }

[EDITED]

The right way to do that:

tabPresentation.setOnSelectionChanged(new EventHandler<Event>() {
            @Override
            public void handle(Event t) {
                if (tabPresentation.isSelected()) {
                    comboBoxPresYear.setVisible(true);
                    lblPresYear.setVisible(true);
                }
            }
        });
Budde answered 4/2, 2013 at 16:35 Comment(0)
P
15

I'm not sure what you're trying to do/ what ActionEvent you are expecting but try either something like:

tabPane.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> ov, Number oldValue, Number newValue) {
        // do something...
    }
}); 

if you want to do something when the selection changes (any tab)

or try:

http://docs.oracle.com/javafx/2/api/javafx/scene/control/Tab.html#setOnSelectionChanged%28javafx.event.EventHandler%29

for a specific tab (I haven't tried this yet, though).

Pancho answered 4/2, 2013 at 17:27 Comment(3)
What I need is when I select a specific tab (tabPress) set one comboBox and one label Visible, just itBudde
just add if(newValue = yourSpecificTabNUmber) { comboBoxPresYear.setVisible(true); lblPresYear.setVisible(true);} to changed method :)Jabiru
Did you try Tab.setOnSelectionChanged I mentioned?Pancho
K
13

You do that with the selectedItemProperty or selectedIndexProperty like Puce was saying. Here the solution with the selectedItemProperty which I think is better because you get the selected Tab item itself

tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {

    @Override
    public void changed(ObservableValue<? extends Tab> observable, Tab oldTab, Tab newTab) {
        if(newTab == tabPresentation) {
            comboBoxPresYear.setVisible(true);
            lblPresYear.setVisible(true);}
        }
    });

I assume the tabPresentation is the Tab object itself.

Kathlenekathlin answered 11/10, 2013 at 5:6 Comment(1)
This is really nice, because I get the tab item directly and don't need to refer to a specific tab number, which might change.Sensuous
S
2

Wouldn't this be the easiest way to do it

    tab.selectedProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue) {
            comboBoxPresYear.setVisible(true);
            lblPresYear.setVisible(true);}
        }
    });
Skindeep answered 4/1, 2018 at 22:35 Comment(1)
That's a great JavaFX8 solutionChaker
W
0

import javafx.event.Event; import javafx.event.EventHandler;

    tab1.setOnSelectionChanged((EventHandler<Event>) t -> {
        if (tab1.isSelected()) {
            System.out.println(tab1.getText() + " is selected");
        }
    });
Wilder answered 25/11, 2021 at 0:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.