2016-12-26 71 views
0

我在舞臺上添加一個滾動條,我想固定在舞臺右側的滾動條,首先我想嘗試通過CSS,但我可以沒有找到像「position:absolute」這樣的風格,所以我在舞臺上添加了一個ChangeListener,當舞臺調整大小時,重置了滾動條的LayoutX和LayoutY,並且是,它可以工作,但是當我最大化舞臺時,它不起作用,在舞臺上收聽如何在javaFX的舞臺右側固定滾動條

stage.heightProperty().addListener(new ChangeListener<Number>() 
      { 
       @Override 
       public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) 
       { 
        System.out.println("this is height,newValue:"+newValue.intValue()+"oldValue:"+oldValue.intValue()); 
       } 
      }); 

stage.maximizedProperty().addListener(new ChangeListener<Boolean>() { 
       @Override 
       public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) { 
        System.out.println("current height"+stage.heightProperty().doubleValue()); 
        System.out.println("current width"+stage.widthProperty().doubleValue()); 
       } 
      }); 

    stage.widthProperty().addListener(new ChangeListener<Number>() 
      { 
       @Override 
       public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) 
       { 
        hBox.setPrefWidth(newValue.intValue()-30); 
        table1HorizontalScrollBar = findScrollBar(table1, Orientation.HORIZONTAL); 
        table1VerticalScrollBar = findScrollBar(table1, Orientation.VERTICAL); 
        table2.setPrefWidth(newValue.intValue()-table1.getWidth()); 
        table2HorizontalScrollBar = findScrollBar(table2, Orientation.HORIZONTAL); 
        table2VerticalScrollBar = findScrollBar(table2, Orientation.VERTICAL); 
        bindValueProperty(); 
        System.out.println("this is width ,newValue:"+newValue.intValue()+"oldValue:"+oldValue.intValue()); 
       } 
      }); 

時,我最大的階段,控制檯輸出如下:

the result on console

現在,我可以得到舞臺的寬度和高度的新值; 但在舞臺上,滾動條仍然在舊的位置,這樣

the positon of scrollbar

回答

0

只要使用正確的佈局適合這個工作:

@Override 
public void start(Stage primaryStage) { 
    ScrollBar scrollBar = new ScrollBar(); 
    scrollBar.setOrientation(Orientation.VERTICAL); 
    AnchorPane root = new AnchorPane(scrollBar); 
    AnchorPane.setTopAnchor(scrollBar, 0d); 
    AnchorPane.setRightAnchor(scrollBar, 0d); 
    AnchorPane.setBottomAnchor(scrollBar, 0d); 

    Scene scene = new Scene(root); 

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

或2個ScrollBar S:

@Override 
public void start(Stage primaryStage) { 
    ScrollBar vScrollBar = new ScrollBar(); 
    vScrollBar.setOrientation(Orientation.VERTICAL); 
    ScrollBar hScrollBar = new ScrollBar(); 
    hScrollBar.setOrientation(Orientation.HORIZONTAL); 

    Region r = new Region(); 
    r.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY))); 
    r.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT))); 

    GridPane root = new GridPane(); 

    ColumnConstraints c1 = new ColumnConstraints(); 
    c1.setHgrow(Priority.ALWAYS); 
    ColumnConstraints c2 = new ColumnConstraints(); 
    c2.setHgrow(Priority.NEVER); 
    root.getColumnConstraints().addAll(c1, c2); 

    RowConstraints r1 = new RowConstraints(); 
    r1.setVgrow(Priority.ALWAYS); 
    RowConstraints r2 = new RowConstraints(); 
    r2.setVgrow(Priority.NEVER); 
    root.getRowConstraints().addAll(r1, r2); 
    root.addRow(0, r, vScrollBar); 
    root.add(hScrollBar, 0, 1); 

    Scene scene = new Scene(root); 

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