2016-05-24 12 views
1

JavaFXでポップアウトウィンドウの例を探しています。 私はJavaFXアプリケーションを持っており、ある時点ではポップアウトウィンドウが表示される必要があります。このポップアウトウィンドウには、処理して確認してメインのアプリケーション/ウィンドウに戻ってくる必要のある複雑な入力が必要です。JavaFX新しいカスタムポップアウトウィンドウ

今、問題はどのJavaFXコントローラクラスでもJavaFXのポップアウトウィンドウを呼び出す方法を見つけることができません。私はDialogをウィンドウをポップアウトする方法を見つけるだけですが、JavaFXに基づいた新しいポップアウトウィンドウの例は見つかりません(パラレルに2つのウィンドウがあるところがありますが、必要なときだけ作成する必要があります) 。

このようなJavaFxカスタムポップアップウィンドウの例をご存知ですか?私はここで、私はあなたが欲しいものを理解だと思う

+0

私はあなたがポップを意味推測***アップ*** ... – fabian

+0

あなたは別の段階では、あなたのポップアップコンテンツを維持してオープンすることができますあなたのポップアップでこのステージを閉じるために 'OK 'ボタンを押してください。 –

+0

これは役立ちますhttp://stackoverflow.com/questions/22166610/how-to-create-a- popup-windows-in-javafxとhttp://stackoverflow.com/questions/12717969/javafx-2-custom-popup-pane –

答えて

4

は(回避策)の例である:

  • 私は2つのFXMLファイル、メインウィンドウ(MainWindow.fxml)用とポップアップウィンドウのための1つを作成しました( Popup.fxml)
  • fxmlファイルごとに2つのコントローラクラスが作成され、これらのコントローラはAbstractContollerクラスを拡張し、興味深いものはすべてこれらの2つのコントローラに入ります。
  • 抽象コントローラクラスは、ちょうどメインウィンドウのコントローラを搭載し、主要ステージシーンのルートとしてメインウィンドウを設定し、具体的なコントローラは、メインアプリケーションへのアクセス
  • mainAppですクラスの派手なことは何もすることができますただ一つの方法を持っています。

source code on github

Screenshot

MainWindow.fxml

<?import javafx.geometry.*?> 
<?import javafx.scene.text.*?> 
<?import javafx.scene.control.*?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.layout.AnchorPane?> 


<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <Label text="I'm the MAiN here"> 
     <font> 
      <Font size="24.0" /> 
     </font> 
     </Label> 
     <Label text="LETS POP THIS OUT"> 
     <font> 
      <Font size="18.0" /> 
     </font> 
     </Label> 
     <Button fx:id="popitBtn" mnemonicParsing="false" text="NOW"> 
     <font> 
      <Font size="14.0" /> 
     </font> 
     </Button> 
     <Label fx:id="resultLbl" text="I've got this (username: /Password:)"> 
     <VBox.margin> 
      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
     </VBox.margin> 
     </Label> 
    </children> 
    <padding> 
     <Insets bottom="40.0" left="40.0" right="40.0" top="40.0" /> 
    </padding> 
</VBox> 

Popup.fxml

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.geometry.*?> 
<?import javafx.scene.text.*?> 
<?import javafx.scene.control.*?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 


<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <HBox alignment="CENTER" style="-fx-background-color: #e1c1c1;"> 
     <children> 
      <Label text="POPUP WINDOW EXAMPLE" textFill="#752b2b"> 
       <font> 
        <Font size="14.0" /> 
       </font> 
      </Label> 
     </children> 
     </HBox> 
     <HBox alignment="CENTER"> 
     <children> 
      <Label prefWidth="70.0" text="Username" /> 
      <TextField fx:id="usernameTF" promptText="John Doe" /> 
     </children> 
     </HBox> 
     <HBox alignment="CENTER"> 
     <children> 
      <Label prefWidth="70.0" text="Password" /> 
      <PasswordField fx:id="passwordPF" promptText="*********" /> 
     </children> 
     </HBox> 
     <HBox alignment="CENTER"> 
     <children> 
      <Button fx:id="connectBtn" mnemonicParsing="false" text="Connect" /> 
     </children> 
     </HBox> 
    </children> 
    <padding> 
     <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" /> 
    </padding> 
</VBox> 

AbstractController.java

public abstract class AbstractController { 

    protected MainApp main; 

    public void setMainApp(MainApp main) { 
     this.main = main; 
    } 
} 

MainApp.java

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class MainApp extends Application { 
     private Stage primaryStage; 

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

     @Override 
     public void start(Stage primaryStage) throws Exception { 
      this.primaryStage = primaryStage; 

      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(getClass().getResource("MainWindow.fxml")); 
      MainWindowController mainWindowController = new MainWindowController(); 
      mainWindowController.setMainApp(this); 
      loader.setController(mainWindowController); 
      Parent layout = loader.load(); 

      Scene scene = new Scene(layout); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } 

     public Stage getPrimaryStage() { 
      return primaryStage; 
     } 
} 

MainWindowController.java

import java.io.IOException; 
    import java.net.URL; 
    import java.util.HashMap; 
    import java.util.ResourceBundle; 

    import javafx.fxml.FXML; 
    import javafx.fxml.FXMLLoader; 
    import javafx.fxml.Initializable; 
    import javafx.scene.Parent; 
    import javafx.scene.Scene; 
    import javafx.scene.control.Alert; 
    import javafx.scene.control.Alert.AlertType; 
    import javafx.scene.control.Button; 
    import javafx.scene.control.Label; 
    import javafx.stage.Modality; 
    import javafx.stage.Stage; 

    public class MainWindowController extends AbstractController implements Initializable { 

     @FXML private Button popitBtn; 
     @FXML private Label resultLbl; 

     @Override 
     public void initialize(URL url, ResourceBundle rb) { 
      resultLbl.setText("Lets get something in here"); 
      popitBtn.setOnAction((event)->{ 
       HashMap<String, Object> resultMap = showPopupWindow(); 
       resultLbl.setText("I've got this (username: "+resultMap.get("username") 
         +" /Password: "+resultMap.get("password")+")"); 
      }); 

     } 


     private HashMap<String, Object> showPopupWindow() { 
      HashMap<String, Object> resultMap = new HashMap<String, Object>(); 

      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(getClass().getResource("Popup.fxml")); 
      // initializing the controller 
      PopupController popupController = new PopupController(); 
      loader.setController(popupController); 
      Parent layout; 
      try { 
       layout = loader.load(); 
       Scene scene = new Scene(layout); 
       // this is the popup stage 
       Stage popupStage = new Stage(); 
       // Giving the popup controller access to the popup stage (to allow the controller to close the stage) 
       popupController.setStage(popupStage); 
       if(this.main!=null) { 
        popupStage.initOwner(main.getPrimaryStage()); 
       } 
       popupStage.initModality(Modality.WINDOW_MODAL); 
       popupStage.setScene(scene); 
       popupStage.showAndWait(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return popupController.getResult(); 
     } 
    } 

PopupController.java

import java.net.URL; 
import java.util.HashMap; 
import java.util.ResourceBundle; 

import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Button; 
import javafx.scene.control.PasswordField; 
import javafx.scene.control.TextField; 
import javafx.stage.Stage; 

public class PopupController extends AbstractController implements Initializable { 

    @FXML private TextField usernameTF; 
    @FXML private PasswordField passwordPF; 
    @FXML private Button connectBtn; 
    private Stage stage = null; 
    private HashMap<String, Object> result = new HashMap<String, Object>(); 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     connectBtn.setOnAction((event)->{ 
      result.clear(); 
      result.put("username", usernameTF.getText()); 
      result.put("password", passwordPF.getText()); 
      closeStage(); 
     }); 

    } 

    public HashMap<String, Object> getResult() { 
     return this.result; 
    } 

    /** 
    * setting the stage of this view 
    * @param stage 
    */ 
    public void setStage(Stage stage) { 
     this.stage = stage; 
    } 

    /** 
    * Closes the stage of this view 
    */ 
    private void closeStage() { 
     if(stage!=null) { 
      stage.close(); 
     } 
    } 

} 
+0

これはまさに私が探していたものです。ありがとうございました! – Demosten

+0

あなたar e welcome :)あなたがそうだと思うなら、この答えを正しいものとしてマークすることを忘れないでください;) –

+0

この例題はうまく動作し、githubファイルはうまく機能しています。 – Hemantha

0

KhalidのSAB「S答えは素晴らしいです。 sceneBuilderを使用してfxmlファイルを作成する場合は、setController(関数showPopupWindow())を設定しないでください。

はちょうど使用 -

FXMLLoaderローダー=新しいFXMLLoader(のgetClass()のgetResource( "FXMLを。ファイル "));。

FXMLファイルは自動的に関連するクラスを初期化するので

関連する問題