0

I have this code which resizes tab when it's clicked twice no matter where I click on the tab.

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());
                    }
                }
            }
        });

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

Can you tell me how I can modify the code in a way that I can size the tab only when I double click on a tab name?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • Use setGraphic(Node node); method of Tab and add listner to that node. See your ques http://stackoverflow.com/a/17104158/1795947 – Shreyas Dave Jun 17 '13 at 06:12
  • yes but as far as I understand when I use `setGraphic(Node node); ` I have to create `setOnMouseClicked` for every tab. How I can do this globally by only adding `setOnMouseClicked` to `tabPane`? – Peter Penzov Jun 17 '13 at 07:08
  • this is not reasonable. You would have to manually check which label was hit. Go for Dave's answer. – zhujik Jun 17 '13 at 07:44

1 Answers1

1

to make it simple,

Create a EventHandler

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


                 ...

              } }
            }
        }

And add this eventhandler to every tab's node.

node.setOnMouseClicked(eventHandler);

This is the only choice you have.