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

Binding to Scrollpane.prefViewportHeightProperty doesn't work as expected

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • 8
    • 7u21
    • javafx
    • JDK 7u21 on Windows 7 64-bit
      JavaFX 2.2.21

      The following code tries to limit the size of the viewport, while adjusting it to match the content when possible... Even though the preferred size changes, it won't adjust the size of the viewport except in specific circumstances that I am so far unable to trigger with my code:

      package viewportsize;

      import javafx.application.Application;
      import javafx.beans.binding.Bindings;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.Label;
      import javafx.scene.control.ScrollPane;
      import javafx.scene.control.TitledPane;
      import javafx.scene.layout.AnchorPane;
      import javafx.scene.layout.Priority;
      import javafx.scene.layout.VBox;
      import javafx.scene.shape.Rectangle;
      import javafx.stage.Stage;

      public class ViewportSize extends Application {
      VBox content = new VBox();

      @Override
      public void start(Stage primaryStage) {
      content.setStyle("-fx-border-color:green;");
      Button button = new Button("Grow Content");
      button.setOnAction(new EventHandler<ActionEvent>() {

      @Override
      public void handle(ActionEvent t) {
      Rectangle r = new Rectangle(100,100);
      r.setStyle("-fx-stroke:red;");
      content.getChildren().add(r);
      }
      });

      ScrollPane scrollPane = new ScrollPane();
      scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
      scrollPane.setContent(content);

      TitledPane selectionTP = new TitledPane("Look here", scrollPane);
      AnchorPane.setBottomAnchor(selectionTP, 0.0);
      AnchorPane.setRightAnchor(selectionTP, 0.0);

      AnchorPane ap = new AnchorPane();

      ap.getChildren().add(selectionTP);
      VBox root = new VBox(4);
      root.getChildren().add(button);
      Label lab = new Label("Hit button a few times and resize vertically. "
      + "Scrollpane size should grow/shrink during resize, if there is "
      + "content to show. But it doesn't, until you adjust the scrollbar "
      + "and then resize the window. Then it will snap to the correct size "
      + "on the next resize.");
      lab.setWrapText(true);
      root.getChildren().add(lab);
      root.getChildren().add(ap);
      VBox.setVgrow(ap, Priority.ALWAYS);
      Scene scene = new Scene(root, 300, 250);

      // ScrollPane viewport height should be clipped at 60 px less than the
      // AnchorPane height, but try to expand vertically as much as it can to
      // accomodate the content.
      scrollPane.prefViewportHeightProperty().bind(Bindings.min(content.heightProperty(),
      Bindings.max(Bindings.subtract(ap.heightProperty(),60.0), 16.0)
      ));

      scrollPane.prefViewportWidthProperty().bind(content.widthProperty());

      primaryStage.setTitle("ScrollPane Viewport preferred height");
      primaryStage.setScene(scene);
      primaryStage.show();
      }

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

            miflemi Mick Fleming
            swpalmer Scott Palmer
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: