2016-08-29 9 views
0

ノード内にPaneを作成すると、画面上にレンダリングせずに幅/高さを計算するにはどうすればよいですか?(JavaFXの)シーンに追加せずにペインサイズを取得しますか?

文書をメモリに作成してプリンタに送信しようとしています。問題は、ページ数を計算する必要があることです。ドキュメントのサイズを取得する必要があります。

簡単な例私が試している:私が今まで出力すべてが>>> 0.0ある

Label testLabel1 = new Label("TEST1"); 
Label testLabel2 = new Label("TEST no. 2"); 
GridPane testGl = new GridPane(); 
testGl.add(testLabel1, 1, 1); 
testGl.add(testLabel2, 2, 2); 
VBox testVBox = new VBox(testGl); 
Pane testPane = new Pane(testVBox); 

//I read that this might be required 
testPane.applyCss(); 
testPane.layout(); 

//Also that a delay is needed for the fx tread to update testPane 
// (but shouldn't this all be in the same thread since it is in the same function? 
// It doesn't seem to help). 
Platform.runLater(()->{ 
    System.out.println(">>> "+ testPane.getBoundsInLocal().getWidth()); 
}); 

。私が開発しているプログラムでは、コンテナ内にPaneが複数あることに注意してください。Paneしたがって、変数testPane

ありがとうございます。

答えて

0

レイアウトが機能するには、をSceneに追加する必要があります。しかしSceneを表示する必要はありませんし、このシーンでPaneを維持する必要はありません:あなたはSceneからルートを削除したい場合は、非でそれを交換する必要があること

Label testLabel1 = new Label("TEST1"); 
Label testLabel2 = new Label("TEST no. 2"); 
GridPane testGl = new GridPane(); 
testGl.add(testLabel1, 1, 1); 
testGl.add(testLabel2, 2, 2); 
VBox testVBox = new VBox(testGl); 
Pane testPane = new Pane(testVBox); 

// add testPane to some scene before layouting 
Scene testScene = new Scene(testPane); 

testPane.applyCss(); 
testPane.layout(); 

System.out.println(">>> " + testPane.getBoundsInLocal().getWidth()); 

// Pane could be removed from scene 
Group replacement = new Group(); 
testScene.setRoot(replacement); 

ヌルノード。

+0

ありがとうございました - 私はいつもSceneの見解を視覚的な要素(例:映画のシーン)とみなしていました。私は、シーンオブジェクトの基本も見直す必要があります。私の他の仕事が呼び出されるので、明日テストしなければならないでしょう。 –

関連する問題