2017-02-10 13 views
0

ScrollPaneを親コンテナに合わせてサイズを変更したい。私はこのコードをテストした:ScrollPaneをjavafxの親に合わせる

@Override 
    public void start(Stage stage) throws Exception { 

     VBox vb = new VBox(); 
     vb.setPrefSize(600, 600); 
     vb.setMaxSize(600, 600); 

     ScrollPane scrollPane = new ScrollPane(); 
     scrollPane.setFitToHeight(false); 
     scrollPane.setFitToWidth(false); 

     scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED); 
     scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED); 

     VBox vb2 = new VBox(); 

     vb.getChildren().add(scrollPane); 
     scrollPane.getChildren().add(vb2); 

     Scene scene = new Scene(vb); 

     stage.setScene(scene); 
     stage.show(); 
    } 

は今、私はスクロール幅、外側のVBox(VB)と同じ高さにしたいです。しかし、私は失敗しました!誰でも助けてくれますか?

答えて

1

まず第一にそれをしない:自分自身へのVBox「VB」を追加

vb.getChildren().add(vb); 

は、例外が発生し、意味がないでしょう:D

は、第二にAnchorPaneを使用して制約を設定しますこのようなScrollPaneのために:ScrollPanegetChildren()メソッドを呼び出すことはできませんので

//Create a new AnchorPane 
AnchorPane anchorPane = new AnchorPane(); 

//Put the AnchorPane inside the VBox 
vb.getChildren().add(anchorPane); 

//Fill the AnchorPane with the ScrollPane and set the Anchors to 0.0 
//That way the ScrollPane will take the full size of the Parent of 
//the AnchorPane (here the VBox) 
anchorPane.getChildren().add(scrollPane); 
AnchorPane.setTopAnchor(scrollPane, 0.0); 
AnchorPane.setBottomAnchor(scrollPane, 0.0); 
AnchorPane.setLeftAnchor(scrollPane, 0.0); 
AnchorPane.setRightAnchor(scrollPane, 0.0); 
//Add content ScrollPane 
scrollPane.getChildren().add(vb2); 
0

は最初に、あなたのコードもコンパイルされません、それはACCを保護していますエッセンス代わりにscrollPane.setContent(vb2);を使用してください。

vb.getChildren().add(vb);は、Nodeを自分自身に追加しようとしているため意味がありません。

vb.getChildren().add(scrollPane); 
VBox.setVgrow(scrollPane, Priority.ALWAYS); 
scrollPane.setMaxWidth(Double.MAX_VALUE); 

scrollPane.setContent(vb2); 
:あなたは次のコード VBoxサイズの使用に合わせて ScrollPaneをしたい場合は java.lang.IllegalArgumentException: Children: cycle detected:

次がスローされます

関連する問題