Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
751 views
in Technique[技术] by (71.8m points)

java - Resize JavaFX tab when double click

I have this simple example with JavaFX tabs:

    public class test extends Application
{

    private BorderPane root;

    // Navigation Utilization
    private ActionTabs actiontabs;

    @Override
    public void start(Stage primaryStage)
    {

        // Set Main Window Label
        primaryStage.setTitle("Desktop Client");
        Image iv = new Image(getClass().getResource("/images/internet-icon.png").toExternalForm());
        primaryStage.getIcons().add(iv);

        root = new BorderPane();
        root.setLeft(getLeftHBox(primaryStage, root));
        Scene scene = new Scene(root, 1000, 1000, Color.WHITESMOKE);  // Set main Stage color

        primaryStage.setScene(scene);

        Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();

        // Set Stage boundaries to visible bounds of the main screen
        primaryStage.setX(primaryScreenBounds.getMinX());
        primaryStage.setY(primaryScreenBounds.getMinY());
        primaryStage.setWidth(primaryScreenBounds.getWidth());  // Maximum width of the display
        primaryStage.setHeight(primaryScreenBounds.getHeight());    // Maximum height of the display
        primaryStage.show();

    }

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

    private HBox getLeftHBox(Stage primaryStage, BorderPane root)
    {
        HBox hbox = new HBox();

        TabPane tabPane = new TabPane();
        BorderPane mainPane = new BorderPane();

        tabPane.setStyle("-fx-font-size: 12pt;"); // Set global size for the font
        // Create Tabs
        Tab tabA = new Tab();
        tabA.setText("Main Component");
        tabA.setStyle("-fx-font-size: 12pt;"); // Set size of the tab name
        tabA.setClosable(false); 
        // Add something in Tab
        StackPane tabA_stack = new StackPane();
        tabA_stack.setAlignment(Pos.CENTER);
        tabA_stack.getChildren().add(new Label("Label@Tab A"));
        tabA.setContent(tabA_stack);
        tabPane.getTabs().add(tabA);

        Tab tabB = new Tab();
        tabB.setText("Second Component");
        tabB.setStyle("-fx-font-size: 12pt;"); // Set size of the tab name
        tabB.setClosable(false); 
        // Add something in Tab
        StackPane tabB_stack = new StackPane();
        tabB_stack.setAlignment(Pos.CENTER);
        tabB_stack.getChildren().add(new Label("Label@Tab B"));
        tabB.setContent(tabB_stack);
        tabPane.getTabs().add(tabB);

        Tab tabC = new Tab();
        tabC.setText("Last Component");
        tabC.setStyle("-fx-font-size: 12pt;"); // Set size of the tab name
        tabC.setClosable(false); // da se mahne opciqta da se zatvarq tab
        // Add something in Tab
        StackPane tabC_vBox = new StackPane();
        tabC_vBox.setAlignment(Pos.CENTER);
        tabC_vBox.getChildren().add(new Label("Label@Tab C"));
        tabC.setContent(tabC_vBox);
        tabPane.getTabs().add(tabC);

        mainPane.setCenter(tabPane);

        mainPane.setPrefSize(300, 500);
        //mainPane.setLayoutX(5);     // Horizontal Position
        mainPane.setLayoutY(32);    // Vertical Position

        hbox.getChildren().addAll(mainPane);

        return hbox;
    }


}

I want when I double click on a tab name to maximize the size of the body of the tab and make it the same width and height as the size of the application. Similar for example to Eclipse IDE tabs. Is this possible with JavaFX?

EDIT

This is the code that I have tested.

public BorderPane initNavigation(Stage primaryStage)
    {

        VBox stackedTitledPanes = createStackedTitledPanes();

        ScrollPane scroll = makeScrollable(stackedTitledPanes);

        final TabPane tabPane = new TabPane();
        final BorderPane mainPane = new BorderPane();

        final Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();

        tabPane.setStyle("-fx-font-size: 12pt;"); // Set global size for the font
        // Create Tabs
        Tab tabA = new Tab();

        tabPane.setOnMouseClicked(new EventHandler<MouseEvent>()
        {
            private double sizeX, sizeY;
            private boolean first = true;

            @Override
            public void handle(MouseEvent me)
            {
                if (first)
                {
                    sizeX = mainPane.getWidth();
                    sizeY = mainPane.getHeight();
                    first = false;
                }

                if (me.getButton().equals(MouseButton.PRIMARY) && me.getClickCount() % 2 == 0)
                {
                    if (sizeX != mainPane.getWidth() || sizeY != mainPane.getHeight())
                    {
                        mainPane.setPrefSize(sizeX, sizeY);

                    }
                    else
                    {
                        mainPane.setPrefSize(primaryScreenBounds.getWidth(), primaryScreenBounds.getHeight());
                        //mainPane.toFront();
                    }
                }
            }
        });

        tabA.setText("Main Component");
        tabA.setContextMenu(makeTabContextMenu(tabA, tabPane));  // Set right mouse click menu
        // Add something in Tab
        StackPane tabA_stack = new StackPane();
        tabA_stack.setAlignment(Pos.CENTER);
        tabA_stack.getChildren().add(scroll);
        tabA.setContent(tabA_stack);
        tabPane.getTabs().add(tabA);

        Tab tabB = new Tab();
        tabB.setText("Second Component");
        tabB.setContextMenu(makeTabContextMenu(tabB, tabPane));  // Set right mouse click menu
        // Add something in Tab
        StackPane tabB_stack = new StackPane();
        tabB_stack.setAlignment(Pos.CENTER);
        tabB_stack.getChildren().add(new Label("Label@Tab B"));
        tabB.setContent(tabB_stack);
        tabPane.getTabs().add(tabB);

        Tab tabC = new Tab();
        tabC.setText("Last Component");
        tabC.setContextMenu(makeTabContextMenu(tabC, tabPane));  // Set right mouse click menu
        // Add something in Tab
        StackPane tabC_vBox = new StackPane();
        tabC_vBox.setAlignment(Pos.CENTER);
        tabC_vBox.getChildren().add(new Label("Label@Tab C"));
        tabC.setContent(tabC_vBox);
        tabPane.getTabs().add(tabC);

        mainPane.setCenter(tabPane);
        mainPane.setPrefSize(300, 500);
        //mainPane.setLayoutX(5);     // Horizontal Position
        mainPane.setLayoutY(32);    // Vertical Position

        scroll.setPrefSize(395, 580);
        scroll.setLayoutX(5);
        scroll.setLayoutY(32);

        return mainPane;
    }

The problem is how I can cover the stage with the tab code when I double click on the tab name?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You required to add few lines to your code, here is a sample for you,

.....

Tab tabA = new Tab();

Label tabALabel = new Label("Main Component");
tabA.setGraphic(tabALabel);

tabALabel.setOnMouseClicked(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent mouseEvent) {
        if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
            if (mouseEvent.getClickCount() == 2) {

                mainPane.setPrefSize(500, 500); //Your required size

            }
       }
    }
});

....

Try this, and tell if there's any difficulty.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...