2012-09-12 11 views
5

私はJavaFXでスケーリング変換を使用しようとしていましたが、私の頭を包み込むことはできませんでした。基本的に、私は複雑なグラフを含むペインを持っていて、それを再スケーリングできるようにしたいと考えています。スケーリング部分自体は正常に動作しますが、囲みスクロールペインはグラフに適応しません。JavaFXとScrollPanesでのスケーリング

public class TestApp extends Application { 
    @Override public void start(final Stage stage) throws Exception { 
     final Label label = new Label("Hello World"); 
     label.getTransforms().setAll(new Scale(0.5, 0.5)); 

     label.setStyle("-fx-background-color:blue"); 
     label.setFont(new Font(200)); 

     final ScrollPane scrollPane = new ScrollPane(); 
     scrollPane.setContent(label); 

     stage.setScene(new Scene(scrollPane)); 
     stage.setWidth(200); 
     stage.setHeight(100); 
     stage.show(); 
    } 

    public static void main(String[] args) { 
     Application.launch(args); 
    } 
} 

は、ラベルが正しく調整されますが、囲んでスクロールペインのバーがまだの構成要素を収容します:

簡単にするため、私は私のグラフはラベルに置き換えた簡単な例を投稿します元のサイズ。

は、私がこれまで試した:

  • むしろ囲むグループをスケーリング(何のスクロールバーが一切表示されません)
  • グループ内のラベルをラップラベルの分と県サイズ
  • で遊んでラベルよりも

私は何が欠けていますか? ScrollPaneをコンテンツビューに適応させるにはどうすればよいですか?

ありがとうございました。

+0

スケーラブル、パン可能スクロール:https://stackoverflow.com/a/44314455/1386911 –

答えて

3

グラフや他のノードのScrollPaneでスケーリングを this example of scrollpane viewports, transforms and layout bounds in JavaFXに実装しました。

コードは私が初めてJavaFXを学習したときに実装されていたので、コードはもっと洗練されているかもしれませんし、おそらくこれを達成するための簡単な方法があります(例えば、ScrollPane documentationで提案されているようにスケールされたノードのコンテナとしてグループを使用する)。あなたがかもしれないのScrollPaneの文書によると

// create a container for the viewable node. 
final StackPane nodeContainer = new StackPane(); 
nodeContainer.getChildren().add(node); 

// place the container in the scrollpane and adjust the pane's viewports as required. 
final ScrollPane scrollPane = new ScrollPane(); 
scrollPane.setContent(nodeContainer); 
scrollPane.viewportBoundsProperty().addListener(
    new ChangeListener<Bounds>() { 
    @Override public void changed(ObservableValue<? extends Bounds> observableValue, Bounds oldBounds, Bounds newBounds) { 
    nodeContainer.setPrefSize(
     Math.max(node.getBoundsInParent().getMaxX(), newBounds.getWidth()), 
     Math.max(node.getBoundsInParent().getMaxY(), newBounds.getHeight()) 
    ); 
    } 
}); 

... 

// adjust the view layout based on the node scalefactor. 
final ToggleButton scale = new ToggleButton("Scale"); 
scale.setOnAction(new EventHandler<ActionEvent>() { 
    @Override public void handle(ActionEvent actionEvent) { 
    if (scale.isSelected()) { 
     node.setScaleX(3); node.setScaleY(3); 
    } else { 
     node.setScaleX(1); node.setScaleY(1); 
    } 
    // runlater as we want to size the container after a layout pass has been performed on the scaled node. 
    Platform.runLater(new Runnable() { 
     @Override public void run() { 
     nodeContainer.setPrefSize(
      Math.max(nodeContainer.getBoundsInParent().getMaxX(), scrollPane.getViewportBounds().getWidth()), 
      Math.max(nodeContainer.getBoundsInParent().getMaxY(), scrollPane.getViewportBounds().getHeight()) 
     ); 
     } 
    }); 
    } 
}); 
+0

は、ご返信いただきありがとうございます。コードでは、いくつかの奇妙な副作用が発生します(たとえば、縮尺を小さくすると、スクロールバーは移動するまで正しく適応しません)。しかし、そのアプローチは間違いありません。これは私を助けました。 – sarcan

8

私は(スクロールバーだけがズームインされ、ノードが見えるビューポートよりも大きい場合に表示される)望んでいた解決策を得るための一つの鍵であった。このコード

グループにペインをラップしようとすると、ScrollPaneは実際のレイアウト境界ではなく視覚的にスクロールされます。

ScrollPane layout calculations are based on the layoutBounds rather than the 
boundsInParent (visual bounds) of the scroll node. If an application wants the 
scrolling to be based on the visual bounds of the node (for scaled content etc.), 
they need to wrap the scroll node in a Group. 
関連する問題