2017-02-19 4 views
-1

メニュー項目のクリックからJavaFXポップアップウィンドウを開く方法を学習しようとしています。私は最終的に新しいウィンドウを開きましたが、モーダルにすることはできません。JavaFXポップアップが動作しない

シーン1つのFXML:

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sceneswitchtest.Scene_1Controller"> 
    <children> 
     <Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" /> 
     <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" /> 
     <MenuBar fx:id="menuBar" layoutX="40.0" layoutY="27.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
     <menus> 
      <Menu mnemonicParsing="false" text="File"> 
      <items> 
       <MenuItem mnemonicParsing="false" text="Close" /> 
        <MenuItem fx:id="MenuItemOpenScene2" mnemonicParsing="false" onAction="#OpenScene2" text="Open Scene 2" /> 
      </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Edit"> 
      <items> 
       <MenuItem mnemonicParsing="false" text="Delete" /> 
      </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Help"> 
      <items> 
       <MenuItem mnemonicParsing="false" text="About" /> 
      </items> 
      </Menu> 
     </menus> 
     </MenuBar> 
    </children> 
</AnchorPane> 

ポップアップFXML:

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

<?import javafx.scene.layout.AnchorPane?> 


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sceneswitchtest.Scene_2Controller"> 

</AnchorPane> 

シーン1つのコントローラ:

package sceneswitchtest; 

import java.io.IOException; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Node; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.MenuBar; 
import javafx.scene.control.MenuItem; 
import javafx.stage.Modality; 
import javafx.stage.Stage; 

public class Scene_1Controller { 

    @FXML 
    private Button button; 

    @FXML 
    private Label label; 

    @FXML 
    private MenuBar menuBar; 

    @FXML 
    private MenuItem MenuItemOpenScene2; 

    @FXML 
    void OpenScene2(ActionEvent event) throws IOException { 
     Stage appStage = (Stage) ((Node) menuBar).getScene().getWindow(); 
     Parent settingsParent = FXMLLoader.load(this.getClass().getResource("Scene_2.fxml")); 
     Scene settingsScene = new Scene(settingsParent); 
     appStage.setScene(settingsScene); 

//  appStage.initModality(Modality.WINDOW_MODAL); 
//  appStage.initOwner(menuBar.getScene().getWindow()); 

     appStage.show(); 
    } 

    @FXML 
    void handleButtonAction(ActionEvent event) throws IOException { 

    } 

} 

注:上記のコードでは、行はコメントここに私のコードですモダリティの使用と同様に失敗しました.APPLICATION_MODAL

シーン2(ポップアップ)コントローラ:

package sceneswitchtest; 


public class Scene_2Controller { 

} 

メイン方法:

パッケージsceneswitchtest。

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

/** 
* 
* @author eric 
*/ 
public class SceneSwitchTest extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("Scene_1.fxml")); 

     Scene scene = new Scene(root); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 
+1

ポップアップウィンドウを表示したいのですか?あなたはポップアップを表示する方法を尋ねるが、あなたのコードはプライマリステージでシーンをスウィッチするだけのようだ。 「ステージ」の所有者をそれ自身に設定することは意味をなさない。プライマリステージの上にポップアップを表示し、 'OpenScene2'メソッドで新しい' Stage'を作成し、モダリティ、所有者、そして 'show()'を設定する必要がある場合。 – MBec

+0

ありがとうございましたMBec、私はあなたが提案した通りに私のOpenScene2メソッドを変更しました! – Luft

+0

ところで、あなたが私の質問に印をつけた人なのかどうかわかりませんが、誰でも私の質問に間違いがないことを指摘してください。私はこのようなことを覚えようとしています。私がそれをする方法を知っていたら、私は尋ねる必要はなかったでしょうか?正当な質問をマークすることにより、あなたはこのサイトが非友好的な純度のスノーによってぶらぶられているという幅広い意見を検証しています。 '言っ途切れる。 – Luft

答えて

0

ここでは、ポップアップを機能させるためにコードを変更しました。

@FXML 
void OpenScene2(ActionEvent event) throws IOException { 
    Parent settingsParent = FXMLLoader.load(this.getClass().getResource("/sceneswitchtest/Scene_2.fxml")); 
    Scene settingsScene = new Scene(settingsParent); 
    Stage popup = new Stage(); 
    popup.setScene(settingsScene); 
    popup.initModality(Modality.WINDOW_MODAL); 
    popup.initOwner(menuBar.getScene().getWindow()); 

    popup.show(); 
} 

私は正しい方向に私を向けるためにMBecに感謝したいと思います。

関連する問題