Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8242547

TabPane selection model issues

XMLWordPrintable

    • x86_64
    • windows_10

      ADDITIONAL SYSTEM INFORMATION :
      com.sun.javafx.runtime.VersionInfo.getRuntimeVersion(): 14+9

      A DESCRIPTION OF THE PROBLEM :
      While trying to solve https://stackoverflow.com/questions/48719039/how-to-hide-tabpane-content-on-tab-clicked-in-javafx I have noticed some strange behavior.
      1. While `tabPane.getSelectionModel().select(-1)` does not notifies selected index property,
      `tabPane.getSelectionModel().clearSelection()` does.

      I have checked javafx.controls sources, clearSelection() calls updateSelectedIndex(-1), and select(-1) calls clearSelection() and return. Really strange behavior.

      2. If clearSelection() were called, listener from selectedIndexProperty called with newValue == -1, but TabPane content stays unchanged.

      Reproducer:
      Run target, click on 'First' tab (now it should be hidden), then click on 'Second' tab (now it should be shown). Notice, that content does not changes (background should be green).


      ---------- BEGIN SOURCE ----------
      package com.wowcube.sdk;

      import javafx.application.*;
      import javafx.geometry.*;
      import javafx.scene.*;
      import javafx.scene.control.*;
      import javafx.scene.layout.*;
      import javafx.stage.*;

      import java.util.concurrent.atomic.AtomicReference;

      public class HideShowTabContentOnClicked extends Application {

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


          private BorderPane createContent() {

              BorderPane borderPane = new BorderPane();

              TabPane tabPane = new TabPane();
              tabPane.setSide(Side.LEFT);

              StackPane stackPane = new StackPane();
              stackPane.setStyle("-fx-background-color: #f00");
              stackPane.setMinWidth(200);

              Tab tab1 = new Tab("First");
              tab1.setClosable(false);
              tab1.setContent(stackPane);

              Tab tab2 = new Tab("Second");
              StackPane stackPane2 = new StackPane();
              stackPane2.setStyle("-fx-background-color: #0f0");
              tab2.setContent(stackPane2);
              tab2.setClosable(false);

              Tab tab3 = new Tab("Third");
              StackPane stackPane3 = new StackPane();
              stackPane3.setStyle("-fx-background-color: #00f");
              tab3.setContent(stackPane3);
              tab3.setClosable(false);

              StackPane center = new StackPane();
              center.setStyle("-fx-background-color: black");

              borderPane.setCenter(center);

              tabPane.getTabs().addAll(tab1, tab2, tab3);

              AtomicReference<Tab> currentTab = new AtomicReference<>(tabPane.getSelectionModel().getSelectedItem());
              AtomicReference<Tab> lastTab = new AtomicReference<>(null);
              tabPane.setOnMouseReleased(event -> {
                  // Check if current node is actually tab
                  Node n = event.getPickResult().getIntersectedNode();
                  while (n != null && !(n.getStyleClass().contains("headers-region"))) {
                      n = n.getParent();
                  }
                  if (n == null)
                      return;

                  lastTab.set(currentTab.get());
                  currentTab.set(tabPane.getSelectionModel().getSelectedItem());

                  if (currentTab.get() == lastTab.get()) {
                      // Hide
                      tabPane.setPrefSize(28, 28);
                      //tabPane.getSelectionModel().select(-1);
                      tabPane.getSelectionModel().clearSelection(); // notify selection model
                      currentTab.set(null);
                  } else {
                      // Show
                      tabPane.setPrefSize(-1,-1);
                      currentTab.set(tabPane.getSelectionModel().getSelectedItem());
                  }
              });

              tabPane.getSelectionModel().selectedIndexProperty().addListener((observable, oldValue, newValue) -> {
                  System.out.println("newValue: "+newValue);
                  if (newValue == null) {
                      System.out.println("Hiding!!!");
                  } else {
                      System.out.println("Showing!!!!!");
                  }
              });

              borderPane.setLeft(tabPane);
              return borderPane;
          }

          @Override
          public void start(Stage stage) throws Exception {
              stage.setWidth(300);
              stage.setHeight(300);
              stage.setScene(new Scene(createContent()));
              //stage.setMaximized(true);
              stage.show();
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            arapte Ambarish Rapte
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: