2017-01-09 21 views
0

私は内部にペインを持つScrollPaneを持っています。最初はペインは空ですが、いくつかのImageViewを動的に(垂直方向に)追加します。画像は、水平にスクロールしなくてもScrollPaneに収まるようにサイズ変更する必要があります。それはスクロール区画の幅に収まるように画像のサイズを変更しませんJavaFX ScrollPaneが幅に適合しない

// Pane that is content of ScrollPane and member variable of object 
private MigPane imagesPane = new MigPane("fill"); 
... 
// creating ScrollPane in some method and add to current view 
ScrollPane spImages = new ScrollPane(); 
spImages.setContent(imagesPane); 
spImages.setFitToWidth(true); 
add(spImages, "wrap"); 
... 
// adding images in another method 
getPm().getImages().forEach(image -> { 
     ImageView img = new ImageView(image); 
     img.setPreserveRatio(true); 
     img.fitWidthProperty().bind(imagesPane.widthProperty()); 
     imagesPane.add(img, "wrap"); 
    }); 

が、なぜ:これは私がこれまで何ですか?私は何が欠けていますか?

答えて

0

画像はcontentだけではなく、高さが、あなたが追加した場合の幅に収まる:

scrollPane.setFitToHeight(true); // it will fits to the height also ! 

とも:

img.fitHeightProperty().bind(imagesPane.heightProperty()); // to bind the height of the imageview to the content's height 

そして、私は知らない何らかの理由この方法setPreserveRatio(true)は問題を引き起こします。 私はのサイズ変更を制限することで画像の品質を保つと思います。画像を取り除くと、画像は2つの水平軸と垂直軸に正しく収まります。

setPreserveRatio:フィッティングバウンディングボックス内の画像に合うようにスケーリングするときに、ソース画像の縦横比を維持するかどうかを示します。

編集:あなたはたとえばVBoxを必要とすることを実現したい場合

それはあなたのレイアウト(コンテンツ)に依存します:

VBox container = new VBox(); 
    ScrollPane sp = new ScrollPane(container); 
    sp.setPrefSize(400, 400); 
    sp.setFitToWidth(true); 

    ImageView img = new ImageView(getClass().getResource("imgA.png").toString()); 
    img.setPreserveRatio(true); 
    img.fitWidthProperty().bind(container.widthProperty()); 

    ImageView img2 = new ImageView(getClass().getResource("imgB.png").toString()); 
    img2.setPreserveRatio(true); 
    img2.fitWidthProperty().bind(container.widthProperty()); 

    container.getChildren().addAll(img,img2); 

幸運!

+0

これは私が望むものではありません。スクロールペインのコンテンツを幅に合わせるだけで縦にスクロールできますが、横にスクロールする必要はありません。 – LPrc

+0

私は上記のあなたのコメントに従って私の答えを編集しました! –

関連する問題