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

Canvas grows but won't shrink

XMLWordPrintable

      package experimental;

      import javafx.application.Application;
      import javafx.geometry.Orientation;
      import javafx.scene.Scene;
      import javafx.scene.canvas.Canvas;
      import javafx.scene.control.SplitPane;
      import javafx.scene.layout.AnchorPane;
      import javafx.stage.Stage;

      public class SplitPaneProblem extends Application {

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

      public SplitPaneProblem() { }

      @Override
      public void start(Stage primaryStage) throws Exception {
      SplitPane splitPane = new SplitPane();
      splitPane.setOrientation(Orientation.VERTICAL);
      splitPane.getItems().add(new TestPane("0",new Canvas()));
      splitPane.getItems().add(new TestPane("1",new Canvas()));
      primaryStage.setScene(new Scene(splitPane));
      primaryStage.setHeight(400);
      primaryStage.setWidth(400);
      primaryStage.show();
      }

      public class TestPane extends AnchorPane {

      String name;
      Canvas canvas;

      public TestPane(String name,Canvas canvas) {
      super();
      this.canvas = canvas;
      getChildren().add(canvas);
      //1 canvas.setManaged(false);
      this.name = name;
      }

      @Override
      public void resize(double width,double height) {
      System.out.println(name+" input "+width+' '+height);
      super.resize(width, height);
      System.out.println(name+" panel "+getWidth()+' '+getHeight());
      //0 canvas.setWidth (width);
      //0 canvas.setHeight(height);
      System.out.println(name+" canvas "+canvas.getWidth()+' '+canvas.getHeight());
      }
      }
      }

      Canvas doesn't resize by itself. Run the code as shown, and drag the divider up and down. Read the console output. The size of the Canvas instances never changes from (0,0).

      Uncomment the lines beginning with //0 and run it again. Now the Canvas instances are forcibly resized to the size of their containing panes. Drag the divider up and down. Note that the Canvas instances grow vertically, but they never shrink, even though the code tells them to. Imagine we were drawing an X across the corners of the Canvases. These X drawings would grow with the divider. But when you drag the divider the other direction, the image is clipped, but it never shrinks.

      Uncomment the line beginning with //1 and run it again. Now it works.

      I don't know about you, but I think this is bizarre. Being a managed component sort of halfway works for Canvas, but not really. The fix shown here was found by accident, and I still have a hard time making myself think this is in any way correct behavior, even after reading a bunch of the relevant docs.

            Unassigned Unassigned
            duke J. Duke
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Imported: