2013-09-21 5 views
0

나는 ImageViews로 채워진 VBox를 가지고 있으며 VBox는 CENTER의 BorderPane 내에 배치되어 있습니다. 이제 ScrollBar를 움직이면 그 값을 얻었고 YBox에 ScrollBar 값만큼의 위치로 VBox를 재배치합니다. 내 코드는 간단하다 :BorderPane 내부에서 VBox를 재배치하는 방법은 무엇입니까?

@Override 
    public void start(Stage stage) throws Exception { 
     stage.setTitle("MyScrollbarSample"); 

     ScrollBar scrollBar = new ScrollBar(); 
     scrollBar.setBlockIncrement(10); 
     scrollBar.setMax(180); 
     scrollBar.setOrientation(Orientation.VERTICAL); 
     // scrollBar.setPrefHeight(180); 
     scrollBar.setValue(90); 

     final VBox vBox = new VBox(1); 
     // vBox.setPrefHeight(180); 
     for (int i = 1; i < 6; i++) { 
      vBox.getChildren().add(new ImageView(new Image(getClass().getResourceAsStream("fw" + i + ".jpg")))); 
     } 

     scrollBar.valueProperty().addListener(new ChangeListener<Number>() { 
      @Override 
      public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) { 
       System.out.println("newValue=" + newValue.doubleValue()); 
//    vBox.setLayoutY(-newValue.doubleValue()); 
       vBox.relocate(vBox.getLayoutX(), - newValue.doubleValue()); 
      } 
     }); 

     BorderPane borderPane = new BorderPane(); 
     borderPane.setCenter(vBox); 
     borderPane.setRight(scrollBar); 
     stage.setScene(new Scene(borderPane)); 
     stage.show(); 
    } 

불행하게도 어느 setLayoutY도 내가 스크롤 막대를 이동할 때 이동하지 않는 한 VBox에 변경합니다.

감사합니다.

답변

1

스크롤바가 뜨는 vbox를 만드는 것처럼 들립니다. 리스너를 사용하는 대신 scrollbar value 속성을 적절한 vbox 레이아웃 차원에 바인딩 할 수 있습니다. 예 : 그냥 borderpane의 중심에서 중 VBOX를 배치하는 경우

ScrollBar bar = new ScrollBar(); 
VBox box = new VBox(); 

box.layoutYProperty().bind(bar.valueProperty()); 

그러나, 이것은 당신이 borderpane에 배치 중 VBOX이 방법에 따라 작동하지 않을 수 있습니다. 이처럼 절대 위치 지정을 할 수 있도록 AnchorPane에 VBox를 래핑해야 할 수도 있습니다.

100 % 확신 할 수는 없지만 시도해 볼 수는 있습니다. 행운을 빕니다.

  • chooks
관련 문제