JimmyD JimmyD - 1 year ago 293
Java Question

Add Buttons to Tabs and Tab area JavaFX

I'm searching a way to add a Button to a JavaFX


Searched the internet for it but I can't find any solution for it.

Something like the buttons in the screenshot below.

Can someone help me with it?

enter image description here

Answer Source

To have your Tabs with iconed Buttons:

The class Tab has a method setGraphic that you can use with any Node to set what shall be displayed on the Tab. Therefore you can use a Button as it is also a Node.

After this you will have a fully functional button, but it will not display any Image as you need in the question. But the class Button also has the method setGraphic which works the same. Therefore you can use an ImageView to display an Image on your button.

To have control Buttons on the top-right corner:

You could try to not place these buttons inside the TabPane but on the TabPane. For this you can use an AnchorPane and anchor the button to the top-right.

An example:


public class Main extends Application {
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root, 400, 400);

            TabPane tabPane = new TabPane();

            // HBox of control buttons
            HBox hbox = new HBox();
            hbox.getChildren().addAll(createButton("min.png"), createButton("max.png"));

            // Anchor the controls
            AnchorPane anchor = new AnchorPane();
            anchor.getChildren().addAll(tabPane, hbox);
            AnchorPane.setTopAnchor(hbox, 3.0);
            AnchorPane.setRightAnchor(hbox, 5.0);
            AnchorPane.setTopAnchor(tabPane, 1.0);
            AnchorPane.setRightAnchor(tabPane, 1.0);
            AnchorPane.setLeftAnchor(tabPane, 1.0);
            AnchorPane.setBottomAnchor(tabPane, 1.0);

            for (int i = 0; i < 2; i++) {
                Tab tab = new Tab("Tab" + (i + 1));

                Button b = createButton("sign.png");

                b.setOnAction(e -> System.out.println("I am the button of " + tab.getText()));

        } catch (Exception e) {

    private Button createButton(String iconName) {
        Button button = new Button();
        ImageView imageView = new ImageView(new Image(getClass().getResource(iconName).toExternalForm()));
        return button;

    public static void main(String[] args) {

The only thing left to remove the default background and borders from the Button, this cal be done by inserting the following styles into your CSS stylesheet. (this line is already part of Main.java: b.getStyleClass().add("tab-button"); to add this style-class for the button)


.tab-button {
    -fx-border-color: transparent;
    -fx-border-width: 0;
    -fx-background-radius: 0;
    -fx-background-color: transparent;

.tab-button:hover {
    -fx-background-color: white;

The result is this:

enter image description here