ScrollPane.prefViewportWidth == computed size?

Werner Lehmann lehmann at
Mon Jun 3 10:44:17 PDT 2013


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 

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

   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();
     ScrollPane sp = new ScrollPane();
     HBox hb = new HBox();
     primaryStage.setScene(new Scene(hb, 300, 200));;
     System.out.println("before: " + vb.getPrefWidth()); // prints -1.0
     System.out.println("after: " + vb.getPrefWidth()); // prints 164.0


More information about the openjfx-dev mailing list