Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
                }
            }
        });
like image 832
Victor Laerte Avatar asked Feb 04 '13 16:02

Victor Laerte


People also ask

How do I close a Javafx tab?

select(tab); Normally, the tabs can be closed by clicking the (default) close icons in the header of the tab, and "Closed!" is printed to the screen.


2 Answers

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).

like image 190
Puce Avatar answered Nov 01 '22 13:11

Puce


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.

like image 25
yaakuro Avatar answered Nov 01 '22 14:11

yaakuro