2016-11-28 8 views
0

私はいくつかの援助が必要です。私は私が取り組んでいるこのjava fxプロジェクトを持っています。この考え方は、デフォルトのウィンドウクローズボタンの動作を防ぎ、ログインが成功した場合にのみダイアログを閉じます。プログラムでダイアログボックスを閉じるには

以下は、ダイアログを設定してダイアログを閉じることができない方法です。

public class MainController extends Application implements Initializable{ 

// Create a dialog 
private Dialog<Pair<String, String>> dialog = new Dialog<>(); 

/*Login resources*/ 
@FXML // fx:id="loginBtn" 
private Button loginBtn; // Value injected by FXMLLoader 

@FXML // fx:id="usernameTxt" 
private TextField usernameTxt; // Value injected by FXMLLoader 

@FXML // fx:id="loginProgressIndicator" 
private ProgressIndicator loginProgressIndicator; // Value injected by FXMLLoader 

@FXML // fx:id="passwordTxt" 
private PasswordField passwordTxt; // Value injected by FXMLLoader 

@FXML // fx:id="statusLbl" 
private Label statusLbl; // Value injected by FXMLLoader 

@FXML 
void login(ActionEvent event) { 

    Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow(); 

    stage.fireEvent(
      new WindowEvent(
        stage, 
        WindowEvent.WINDOW_CLOSE_REQUEST 
      ) 
    ); 
} 

/*Main Menu resources*/ 
@FXML // ResourceBundle that was given to the FXMLLoader 
private ResourceBundle resources; 

@FXML // URL location of the FXML file that was given to the FXMLLoader 
private URL location; 

@FXML // fx:id="visitBtn" 
private Button visitBtn; // Value injected by FXMLLoader 

@FXML // fx:id="reportBtn" 
private Button reportBtn; // Value injected by FXMLLoader 

@FXML // fx:id="setupBtn" 
private Button setupBtn; // Value injected by FXMLLoader 

@FXML 
void report(ActionEvent event) { 

} 

@FXML 
void setup(ActionEvent event) { 

} 

@FXML 
void visit(ActionEvent event) { 
    System.out.println("Visit Button Pressed."); 
} 

/** 
* The main entry point for all JavaFX applications. 
* The start method is called after the init method has returned, 
* and after the system is ready for the application to begin running. 
* <p> 
* <p> 
* NOTE: This method is called on the JavaFX Application Thread. 
* </p> 
* 
* @param primaryStage the primary stage for this application, onto which 
*      the application scene can be set. The primary stage will be embedded in 
*      the browser if the application was launched as an applet. 
*      Applications may create other stages, if needed, but they will not be 
*      primary stages and will not be embedded in the browser. 
*/ 
@Override 
public void start(Stage primaryStage) throws Exception { 

    Parent root = FXMLLoader.load(getClass().getResource("main.fxml")); 
    Scene scene = new Scene(root); 

    primaryStage.setTitle("Main Menu - PEHCS Point of Sale"); 

    primaryStage.setScene(scene); 
    primaryStage.show(); //This will display the main menu 

    dialog.setTitle("Login - PEHCS Point of Sale"); 
    dialog.setHeaderText(null); 


    // Get the Stage. 
    Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow(); 
    // Add a custom icon. 
    stage.getIcons().add(new Image(this.getClass().getResource("activa.jpg").toString())); 


    /*This section prevents the dialog from closing when we press on the window X button*/ 
    Window window = dialog.getDialogPane().getScene().getWindow(); 
    window.setOnCloseRequest(new EventHandler<WindowEvent>() { 

     /** 
     * Invoked when a specific event of the type for which this handler is 
     * registered happens. 
     * 
     * @param event the event which occurred 
     */ 
     @Override 
     public void handle(WindowEvent event) { 
      event.consume(); 
     } 
    }); 


    /*Set th content of that dialog to be the login.fxml resource file*/ 
    dialog.getDialogPane().setContent(FXMLLoader.load(getClass().getResource("login.fxml"))); 


    Optional<Pair<String, String>> result = dialog.showAndWait(); 

    result.ifPresent(usernamePassword -> { 
     System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue()); 
    }); 

} 

/** 
* Called to initialize a controller after its root element has been 
* completely processed. 
* 
* @param location The location used to resolve relative paths for the root object, or 
*     <tt>null</tt> if the location is not known. 
* @param resources The resources used to localize the root object, or <tt>null</tt> if 
*/ 
@Override 
public void initialize(URL location, ResourceBundle resources) { 
    assert visitBtn != null : "fx:id=\"visitBtn\" was not injected: check your FXML file 'main2.fxml'."; 
    assert reportBtn != null : "fx:id=\"reportBtn\" was not injected: check your FXML file 'main2.fxml'."; 
    assert setupBtn != null : "fx:id=\"setupBtn\" was not injected: check your FXML file 'main2.fxml'."; 

    assert loginBtn != null : "fx:id=\"loginBtn\" was not injected: check your FXML file 'login.fxml'."; 
    assert usernameTxt != null : "fx:id=\"usernameTxt\" was not injected: check your FXML file 'login.fxml'."; 
    assert loginProgressIndicator != null : "fx:id=\"loginProgressIndicator\" was not injected: check your FXML file 'login.fxml'."; 
    assert passwordTxt != null : "fx:id=\"passwordTxt\" was not injected: check your FXML file 'login.fxml'."; 
    assert statusLbl != null : "fx:id=\"statusLbl\" was not injected: check your FXML file 'login.fxml'."; 
} 

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

オン・スタート・メソッドでウィンドウ・クローズ・イベントが消費されています。今私はこのログインダイアログでこのダイアログを閉じたいと思っています。これは私が立ち往生している場所です。私は助けが必要です。

ありがとうございます。

+0

こんにちは。私はダイアログ変数のアドレスを開始時とログイン時に表示しています。つまり、これを設定する変数の使用は、閉じようとするサムの使用ではないため、終了しません。なぜ誰かが私を助けることができますか? –

答えて

0

プログラムでウィンドウを閉じるには、あなたが必要なのはこれが唯一のウィンドウを閉じ外部要求に対して呼び出されonCloseRequestハンドラを、(docsを参照)を誘発しないことを

@FXML 
void login(ActionEvent event) { 

    dialog.close(); 

} 

注意です。

関連する問題