JavaFX: Resize a Circle on the selected Tab

01

02

/*
 * Copyright 2014 Patrick Ott .
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package resizenode;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

/**
 *
 * @author Patrick Ott 
 */
public class ResizeNode extends Application {

    @Override
    public void start(Stage primaryStage) {

        Circle circle = new Circle(100.0);
        Tab tab = new Tab("Tab 1");
        BorderPane borderPane = new BorderPane(circle);
        borderPane.prefHeightProperty().bind(circle.radiusProperty());
        tab.contentProperty().setValue(borderPane);
        Tab tab2 = new Tab("Demo Tab");

        TabPane tabPane = new TabPane();
        tabPane.getTabs().addAll(tab, tab2);
        
        

        Button button = new Button("Resize Circle");
        button.onActionProperty().set((ActionEvent event) -> {
            // get the selected tab which contains the 
            Tab selectedItem = tabPane.getSelectionModel().getSelectedItem();
            // get the index of the selected tab
            int selectedIndex = tabPane.selectionModelProperty().get().getSelectedIndex();
            // remove this tab from the TabPane
            tabPane.getTabs().remove(selectedItem);
            // resize the circle
            circle.radiusProperty().set(150.0);
            // add the BorderPane, which contains the Circle to the Tab
            tab.setContent(borderPane);
            // add the Tab at its old index to the tabPane
            tabPane.getTabs().add(selectedIndex, tab);
            // set the former selected tab selected again
            tabPane.getSelectionModel().select(tab);
        });

        VBox vBox = new VBox(tabPane, button);
        Scene scene = new Scene(vBox, 800, 600);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}