web-dev-qa-db-ja.com

javaFXでシーンを切り替える

MenuItemが選択されているときに、現在のシーンを閉じて別のシーンを開こうとすると問題が発生します。私のメインステージは次のようにコーディングされています。

public void start(Stage primaryStage) throws Exception {
   primaryStage.setTitle("Shop Management");
   Pane myPane = (Pane)FXMLLoader.load(getClass().getResource
("createProduct.fxml"));
   Scene myScene = new Scene(myPane);        
   primaryStage.setScene(myScene);
   primaryStage.show();
}

次に、createProduct.fxml内で、menuItemがonclickの場合、次のように実行されます。

public void gotoCreateCategory(ActionEvent event) throws IOException {
    Stage stage = new Stage();
    stage.setTitle("Shop Management");
    Pane myPane = null;
    myPane = FXMLLoader.load(getClass().getResource("createCategory.fxml"));
    Scene scene = new Scene(myPane);
    stage.setScene(scene);
    stage.show();
}

CreateCategory.fxmlを開きます。ただし、createProduct.fxmlである前のパネルは閉じません。これを行うためにstage.close()と呼ばれるものがあることは知っていますが、最初からメインからシーンを渡さないため、どこに実装するかわかりません。どうすれば修正できるのだろうか。

前もって感謝します。

7
user2424370

Startメソッドにいくつかの変更を加える必要があります。

public void start(Stage primaryStage) throws Exception {
   primaryStage.setTitle("Shop Management");

   FXMLLoader myLoader = new FXMLLoader(getClass().getResource("createProduct.fxml"));

   Pane myPane = (Pane)myLoader.load();

   CreateProductController controller = (CreateProductController) myLoader.getController();

   controller.setPrevStage(primaryStage);

   Scene myScene = new Scene(myPane);        
   primaryStage.setScene(myScene);
   primaryStage.show();
}

createProductController.Javaは、

public class CreateProductController implements Initializable {

    Stage prevStage;

    public void setPrevStage(Stage stage){
         this.prevStage = stage;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }

    public void gotoCreateCategory(ActionEvent event) throws IOException {
       Stage stage = new Stage();
       stage.setTitle("Shop Management");
       Pane myPane = null;
       myPane = FXMLLoader.load(getClass().getResource("createCategory.fxml"));
       Scene scene = new Scene(myPane);
       stage.setScene(scene);

       prevStage.close();

       stage.show();
    }

}
10
Shreyas Dave

あなたはあなたの問題を解決するために複数の方法を持っています、それらのいくつかは私があなたに話します。あなたの質問から私が理解したのは、上部にナビゲーション(メニューバー)を含むアプリケーションを作成し、それを使用することでユーザーは任意の画面に切り替えることができるということです。

最も簡単な方法は、ナビゲーションバーを静的にするか、画面に渡すか、またはその他の方法で、ナビゲーションバーをグローバルに使用できるようにすることです。次に、単一の画面(Border Paneなど)を作成し、その画面(中央)に他の画面を読み込みます。

プロジェクトでスプリング統合を使用して、コントローラーの注入を改善することもできます。参照を参照してください: http://www.zenjava.com/2011/10/25/views-within-views-controllers-within-controllers/

独自の単一画面コントローラーを作成し、それを使用して他の画面を管理することもできます。参照: https://blogs.Oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx1

ただし、これ(上記)では、現在のアーキテクチャを変更または更新する必要があります。

2
Shreyas Dave

現在開いているウィンドウ/ステージはfx:idから取得できます。 createProduct.fxmlに、以下のようなfx:idのアイテムがあることを確認してください。

<TextField fx:id="username" labelFloat="true" layoutX="80.0" layoutY="300.0" prefHeight="32.0" prefWidth="260.0" promptText="Login Username" />

fx:id属性が表示されることを願っています。

次に、スタートコントローラで、そのままにします

    public void start(Stage primaryStage) throws Exception {
   primaryStage.setTitle("Shop Management");
   Pane myPane = (Pane)FXMLLoader.load(getClass().getResource("createProduct.fxml"));
   Scene myScene = new Scene(myPane);        
   primaryStage.setScene(myScene);
   primaryStage.show();
}

次に、CreateProductController.Javaで

//make sure you import the javafx item you have used, eg have used TextField
import javafx.scene.control.TextField;

    public class CreateProductController implements Initializable {
// make sure the variable has the same type as the item in the fxml "TextField" and same name "username"
    @FXML
    private TextField username;
//we use the above variable since its referencing the window to which it belongs because of the fx:id attribute, then we close it
    private void closeStage() 
    {
      ((Stage) username.getScene().getWindow()).close();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }

    public void gotoCreateCategory(ActionEvent event) throws IOException {
       closeStage();// we close the old stage
       Stage stage = new Stage();
       stage.setTitle("Shop Management");
       Pane myPane = null;
       myPane = FXMLLoader.load(getClass().getResource("createCategory.fxml"));
       Scene scene = new Scene(myPane);
       stage.setScene(scene);    
       stage.show();
    }
}
0
MAYOBYO HASSAN