ScrollPane.prefViewportWidth == computed size?

Werner Lehmann lehmann at media-interactive.de
Mon Jun 3 10:44:17 PDT 2013


Hi,

I am trying to show a ScrollPane resized so that the full width of its 
content is visible: no horizontal scrolling, no clipping, no content 
resizing. Only vertical scrolling if necessary.

Seems as if prefViewportWidth should do the trick. Since I don't want to 
hardcode some width I am binding it to the prefWidth of the scrollpane 
content.

Problem: this works only if the content prefWidth is set explicitly. If 
it is -1, prefViewportWidth is also -1 (because it is bound) and that 
computes a value too small to fit the (computed) prefWidth of the 
content, leading to a horizontal scrollbar. Is this a bug, or am I 
supposed to do this differently?

Here is a test case. It only works if vb.prefWidth is set explicitly at 
the end. Which of course is not a dynamic value...

public class PrefViewportTest extends Application
{
   public static void main(String[] args) { Application.launch(args); }

   @Override
   public void start(Stage primaryStage) throws Exception
   {
     // Scene -> HBox -> ScrollPane -> VBox -> Label
     Label l = new Label("some boring not too short text");
     VBox vb = new VBox();
     vb.getChildren().add(l);
     ScrollPane sp = new ScrollPane();
     sp.setContent(vb);
     sp.prefViewportWidthProperty().bind(vb.prefWidthProperty());
     HBox hb = new HBox();
     hb.getChildren().add(sp);
     primaryStage.setScene(new Scene(hb, 300, 200));
     primaryStage.show();
     System.out.println("before: " + vb.getPrefWidth()); // prints -1.0
     vb.setPrefWidth(vb.prefWidth(-1));
     System.out.println("after: " + vb.getPrefWidth()); // prints 164.0
   }
}

Rgds
Werner


More information about the openjfx-dev mailing list