2016-10-24 10 views
-1

私はmaven、javafx、fxmlのプロジェクトを持っています。 1つのメインのBorderPane、welcome.fxml、およびペイン、ready.fxmlがあります。JavaFX別のペインの同じテキストエリア

私のスタートメソッドは;

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

    try { 
     Pane root = (Pane) FXMLLoader.load(getClass().getResource("welcome.fxml")); 
     Scene scene = new Scene(root, 640, 480); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } catch (Exception e) { 
     makeAlert(e, false); 
    } 
} 

は今、私は私のwelcome.fxmlのボタンを持っている、と私はready.fxmlと私のBorderPaneの中心部を変更したいです。ここに私のボタンハンドラがあります。

@FXML 
private void buttonHandler() throws IOException, InterruptedException { 


    stage = (Stage) myButton.getScene().getWindow(); 
    Pane sub = (Pane) FXMLLoader.load(getClass().getResource("../ready.fxml")); 
    BorderPane root = (BorderPane) FXMLLoader.load(getClass().getResource("../welcome.fxml")); 
    root.setCenter(sub); 

    //Scene scene = new Scene(root, 640, 480); 
    //stage.getScene().setRoot(root); 
} 

UPDATE:ここ@James_Dが気づいたように、私は私のコントローラで再びwelcome.fxmlを読み込むので、私の全体のシーンの変化は、中央のみのinsted、私のミスです。

正しい方法が必要です。

stage = (Stage) brokerConnect.getScene().getWindow(); 
Pane center = (Pane) FXMLLoader.load(getClass().getResource("../ready.fxml")); 
// FIXME: Get root like this 
BorderPane root = (BorderPane) stage.getScene().getRoot(); 
root.setCenter(center); 

EDITED:Javaコードが追加されました。

+0

は、なぜあなたはシーンの全体ルートを交換します窓?それが現れても、あなたの質問は本当に答えには向いていません...私は[MCVE]を作成し、それを含む質問を編集することをお勧めします。あなたが得る行動があなたが望む行動とどのように異なるのかを説明してください。 –

+0

これを実行した後、 'root.setCenter(newPane);'センターが変更されていないので、私はrootSceneを置き換えます。私はちょうどJavaFXを学び始めました、私の無知のために申し訳ありません。 –

+0

あなたが私たちに示していないコードで何か間違ったことをしています。あなたがここで不愉快なJavaFXを知らないわけではありません。誰かがそれに答える必要のある情報なしで質問を投稿したことです。 –

答えて

1

の中心を更新する必要があります。新しい枠を作成せずに、新しい枠の中心を設定するのではなく、の境界枠を更新する必要があります。

通常必要なことは、ボーダーペインをコントローラーに挿入するだけです。だから、welcome.fxmlのルート要素にfx:idを追加します。

<!-- imports, etc... --> 
<BorderPane fx:id="root" fx:controller="..." xmlns:fx="..." ... > 
    <!-- ... --> 
</BorderPane> 

そして、あなたが唯一の境界線の中心を交換したい場合は、コントローラに

public class Controller { /* or whatever name you have, again, you can't be bothered to post a MCVE */ 

    @FXML 
    private BorderPane root ; 

    @FXML 
    private void buttonHandler() throws IOException { 
     Pane sub = (Pane) FXMLLoader.load(getClass().getResource("../ready.fxml")); 
     root.setCenter(sub); 
    } 

    // ... 
} 
関連する問題