2017-06-13 6 views
0

基本的なログインアプリケーションを作成しようとしています。資格情報を入力した後、ユーザーは新しい画面に移動する必要があります。JavaFXが毎回新しい画面を開く

しかし、アプリケーションを実行して資格情報を入力すると、新しい画面が表示されますが、これは新しいウィンドウであり、古いウィンドウがバックグラウンドで開いていることがわかります。

以下

は私のコードです:

package com.gsx.controller; 

import java.io.IOException; 
import java.util.logging.Logger; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 

import application.Main; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.Alert; 
import javafx.scene.control.Alert.AlertType; 
import javafx.scene.control.ButtonType; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.AnchorPane; 
import javafx.stage.Stage; 

public class LoginController { 

private Scene scene; 

@FXML 
private TextField databaseUserName; 

@FXML 
private TextField databasePassword; 

@FXML 
private AnchorPane mainPageAnchorPane; 

private static SessionFactory sessionFactory = null; 


//Controller method to handle the click of the Database Login button 
//This method needs to try and setup a database connection based on the credentials provided 
//and advance to the next stage if it is successfull in establishing the connection 
//If the credentials entered are incorrect an error message must be displayed and the 
//control must remain in the same page. 
@FXML 
public void handleLoginButtonClick(ActionEvent actionEvent){ 



    Session session = null; 

    try{ 

    Configuration configuration = new Configuration(); 
    //configuration.configure("applicationConfig.xml"); 
    configuration.configure("applicationConfig.xml"); 
    configuration.setProperty("hibernate.connection.username",databaseUserName.getText()); 
    configuration.setProperty("hibernate.connection.password",databasePassword.getText()); 
    System.out.println("Configuration was successful"); 
    System.out.println(configuration.getProperty("connection.username")); 
    System.out.println(configuration.getProperty("hibernate.connection.password")); 
    //Create the session factory 
    sessionFactory = configuration.buildSessionFactory(); 

    //Get the session object 
    session = sessionFactory.getCurrentSession(); 



     //Begin the transaction 
     Transaction transaction = session.beginTransaction(); 

     System.out.println("Connection successful"); 
     //some problem here debug this tomorrow 

     AnchorPane root = new AnchorPane(); 
     Scene scene = new Scene(root,650,800); 
     FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/gsx/frontend/mainPage.fxml")); 
     System.out.println(fxmlLoader); 
     scene.setRoot((Parent)fxmlLoader.load()); 
     Main.stage.setScene(scene); 
     Main.stage.show(); 
    } 
    catch(Exception exception){ 

     exception.printStackTrace(); 
     Alert alert = new Alert(AlertType.ERROR, "The login information is incorrect",ButtonType.OK); 
     alert.setTitle("Invalid Cred!!"); 
     alert.show(); 
     if(alert.getResult() == ButtonType.OK){ 

     } 
    } 
    finally{ 
     System.out.println("Finally Block to take care of closing the session and the session Factory"); 
     if(null!=session){ 
      System.out.println("Session is not Null. Closing it"); 
      session.close(); 
     } 

    } 



} 



//Method to show the Login Screen 
public void showLoginScreen(){ 
    try{ 
     FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("login.fxml")); 
     scene.setRoot((Parent)fxmlLoader.load()); 
     } 
    catch(IOException exception){ 
     Logger.getLogger(LoginController.class.getName()); 
    } 
} 

public static SessionFactory getSessionFactory() { 
    return sessionFactory; 
} 

} 

あなたは私が間違ってやっているものに私を助けてくださいことはできますか?

+0

コードにリンクするのではなく、[MCVE]を作成して質問に投稿してください。何らかの理由でリンクが古くなった場合、その質問は他のユーザーには役に立たなくなります。 –

+0

@James_D - 私はあなたがdownvoteを削除してくださいコメントを追加しましたか? –

+0

今後も[MCVE]を投稿してください。あなたが掲示したものは、最小限ではありません(あなたに求めていることとは関係のないデータベース接続など、無関係なコードがすべて含まれています)、完全ではありません(そのままコピー、コンパイル、実行することはできません)。 –

答えて

0

あなたが間違っていること: ログインコントローラでは、ステージクラスの完全な新しいインスタンスを作成します。新しいステージを作成すると、新しいウィンドウに表示されるので、2つの選択肢があります。新しいステージをロードするときに古いステージを閉じるか、メインステージにアクセスしてシーンを設定する必要があります。ステージにアクセスもする

は、次のように使用することができ、これはあなたのログインの段階にアクセスもしますが、同じ論理があなたの主な段階で使用することができます。

databaseUserName.getScene.getWindow 

今、あなたは(.hideを呼び出すためにこれを使用することができます)方法ステージを閉じるか、そのステージで新しいシーンを設定することができます。

いけないあなたがする必要がある場合は常に、(ステージ)に、この

databaseUserName.getScene.getWindow 

をキャストすることができます忘れて、そしてまた、それはnullを返しますので、初期からそれをアクセスもしようといけません。

最後に、afterburnerfxなどのフレームワークを使用することをお勧めします。また、多くのシーンでアプリケーションを作成する予定がある場合は、他のものを使用することをおすすめします。

最後に、あなたのprimaryStageが静的作ることができるが、その

に対して私は強くアドバイス、私はすべてのエラーのためにとても申し訳ありませんが、携帯電話からこれを書きました。それ以上の支援が必要な場合は遠慮なくご相談ください。

+0

はい私はアプリケーションを通して私の主要なステージを使用することにしました。 –

0

あなたは、行の下に、前winodw.After stage.show();使用

mainPageAnchorPane.getScene().getWindow().hide(); 

あなたの前のウィンドウを閉じるには、JavaでLoginControllerに以下のコードを使用することができますclose.Youは、そのウィンドウのanchorpaneのオブジェクトを使用する必要がありますあなたは閉じたいと思っています。

関連する問題