Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the selected Tab from the TabPane with JavaFX?

Tags:

java

tabs

javafx

I am searching for how to get the selected tab from a TabPane just by clicking on a button with JavaFX. I've tried to do it with the ChangeListener but it doesn't work yet.

So how can I do that?

like image 769
MadPro Avatar asked May 16 '15 14:05

MadPro


People also ask

How to use TabPane in JavaFX?

Tabs are added to the TabPane by using the getTabs() . Tabs in a TabPane can be positioned at any of the four sides by specifying the Side . A TabPane has two modes floating or recessed. Applying the styleclass STYLE_CLASS_FLOATING will change the TabPane mode to floating.

How to add tabs to TabPane JavaFX?

Here is an example of adding 3 tabs to a JavaFX TabPane : TabPane tabPane = new TabPane(); Tab tab1 = new Tab("Planes", new Label("Show all planes available")); Tab tab2 = new Tab("Cars" , new Label("Show all cars available")); Tab tab3 = new Tab("Boats" , new Label("Show all boats available")); tabPane. getTabs().

How do you close tabs in TabPane JavaFX?

add(tab); tabPane. getSelectionModel(). 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.

What is group JavaFX?

Group class is a part of JavaFX. A Group contains the number of nodes. A Group will take on the collective bounds of its children and is not directly resizable. Group class inherits Parent class.


1 Answers

As with many JavaFX controls there is a SelectionModel to get first.

tabPane.getSelectionModel().getSelectedItem(); To get the currently selected tab, or alternatively, getSelectedIndex() for the index of the selected tab.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TabSelect extends Application {

    @Override
    public void start(Stage primaryStage) {
        TabPane tabPane = new TabPane();
        tabPane.getTabs().addAll(new Tab("tab 1"),
                                 new Tab("tab 2"),
                                 new Tab("tab 3"));
        Button btn = new Button();
        btn.setText("Which tab?");
        Label label = new Label();
        btn.setOnAction((evt)-> {
            label.setText(tabPane.getSelectionModel().getSelectedItem().getText());
        });

        VBox root = new VBox(10);
        root.getChildren().addAll(tabPane, btn, label);
        Scene scene = new Scene(root, 300, 300);
        primaryStage.setScene(scene);
        primaryStage.show();

        //you can also watch the selectedItemProperty
        tabPane.getSelectionModel().selectedItemProperty().addListener((obs,ov,nv)->{
            primaryStage.setTitle(nv.getText());
        });

    }

    public static void main(String[] args) { launch(args); }

}
like image 94
brian Avatar answered Oct 07 '22 17:10

brian