javafx.stage.Modality
JavaFX 2 でダイアログを出す方法なんですが基本的なコードがforums.oracle.comにありました。
javafx.stage.Popup as JOptionPane
API javafx.stage.Modality
モーダルダイアログの一例です
ModalDialog.java
import javafx.stage.*; import javafx.scene.*; import javafx.scene.paint.Color; import javafx.scene.control.*; import javafx.event.ActionEvent; import javafx.event.EventHandler; public class ModalDialog { private Label label = new Label("What is your name ?"); private Button btn; /* Mainクラスのインスタンス代入用 */ private ModalTest parent; /* MainクラスのStageとMainクラスのインスタンスを受け取るコンストラクタ * この場合はMainクラスをダイアログのownerとしている */ public ModalDialog(final Stage owner_stg, ModalTest mt) { parent = mt; btn = new Button(); final TextField tf = new TextField(); /* ModalDialog にするStage */ final Stage stage = new Stage(); /* modalのタイプでStage(ダイアログ)を初期化 * 他に Modality.WINDOW_MODAL, Modality.NONEがある */ stage.initModality(Modality.APPLICATION_MODAL); //ダイアログのownerをセットする stage.initOwner(owner_stg); stage.setTitle("Dialog Stage"); Group root = new Group(); Scene scene = new Scene(root, 380, 250, Color.web("#0000FF", 0.3)); btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { String name = tf.getText(); parent.setText_TextArea("Hi ! " + name); tf.deleteText(0, name.length()); //stage.hide(); stage.close(); ConfirmModalDialog Confirm_md = new ConfirmModalDialog(name, owner_stg, parent); } }); tf.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { String name2 = tf.getText(); parent.setText_TextArea(name2); tf.deleteText(0, name2.length()); } }); label.setPrefWidth(200.0); label.setLayoutX((scene.getWidth() -200.0) / 2 ); btn.setPrefWidth(50.0); btn.setLayoutX((scene.getWidth() -50.0) / 2 ); label.setLayoutY(scene.getHeight() / 3); btn.setLayoutY(scene.getHeight() / 2); btn.setText("OK"); tf.setPrefColumnCount(30); tf.setLayoutX((scene.getWidth() -330.0) / 2 ); tf.setLayoutY(scene.getHeight() / 2 + 50); root.getChildren().addAll(label, btn, tf); stage.setScene(scene); stage.setResizable(false); stage.show(); } }
ModalTest.java
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextArea; import javafx.scene.paint.Color; import javafx.stage.Stage; public class ModalTest extends Application { private TextArea ta; public static void main(String[] args) { Application.launch(ModalTest.class, args); } @Override public void start(final Stage primaryStage) { final ModalTest mt = this; primaryStage.setTitle("Modal Test"); Group root = new Group(); Scene scene = new Scene(root, 500, 450, Color.LIGHTBLUE); Button btn = new Button(); ta = new TextArea(); ta.setPrefColumnCount(40); ta.setPrefRowCount(22); ta.setLayoutX(20); ta.setLayoutY(10); btn.setLayoutX(200); btn.setLayoutY(400); btn.setText("Show dialog"); btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { ModalDialog md = new ModalDialog(primaryStage, mt); } }); root.getChildren().addAll(ta, btn); primaryStage.setScene(scene); primaryStage.setResizable(false); primaryStage.show(); } public void setText_TextArea(String s){ ta.appendText(s + "\n"); } }
ConfirmModalDialog.java
import javafx.stage.*; import javafx.scene.*; import javafx.scene.paint.Color; import javafx.scene.control.*; import javafx.event.ActionEvent; import javafx.event.EventHandler; public class ConfirmModalDialog { private Label label; private Button btn; private Button yes_btn; private Button no_btn; private ModalTest parent; public ConfirmModalDialog(final String name, final Stage owner_stg, ModalTest mt) { parent = mt; label = new Label(name + ", do you like \"JavaFX\" ?"); yes_btn = new Button(); no_btn = new Button(); final TextField tf = new TextField(); final Stage stage = new Stage(); //Initialize the Stage with type of modal stage.initModality(Modality.WINDOW_MODAL); //Set the owner of the Stage stage.initOwner(owner_stg); stage.setTitle("Confirm Dialog Stage"); Group root = new Group(); Scene scene = new Scene(root, 380, 250, Color.web("#0000FF", 0.3)); yes_btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { parent.setText_TextArea(tf.getText()); parent.setText_TextArea(name + " , Cool !"); stage.hide(); Messege_ModalDialog y_md = new Messege_ModalDialog(name, "yes", owner_stg, parent); } }); no_btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { parent.setText_TextArea(tf.getText()); parent.setText_TextArea(name + " , Bad Boy !"); // stage.hide(); stage.close(); Messege_ModalDialog n_md = new Messege_ModalDialog(name, "no", owner_stg, parent); } }); tf.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { String s2 = tf.getText(); parent.setText_TextArea(s2); tf.deleteText(0, s2.length()); } }); label.setPrefWidth(300.0); label.setLayoutX((scene.getWidth() - 300.0) / 2 ); yes_btn.setPrefWidth(50.0); yes_btn.setLayoutX((scene.getWidth() -50.0) / 3 ); no_btn.setPrefWidth(50.0); no_btn.setLayoutX((scene.getWidth() -50.0) / 3 * 2 ); label.setLayoutY(scene.getHeight() / 3); yes_btn.setLayoutY(scene.getHeight() / 2); yes_btn.setText("YES"); no_btn.setLayoutY(scene.getHeight() / 2); no_btn.setText("NO"); tf.setPrefColumnCount(30); tf.setLayoutX((scene.getWidth() -330.0) / 2 ); tf.setLayoutY(scene.getHeight() / 2 + 50); root.getChildren().addAll(label, yes_btn, no_btn, tf); stage.setScene(scene); stage.setResizable(false); stage.show(); } }
Messege_ModalDialog.java
import javafx.stage.*; import javafx.scene.*; import javafx.scene.paint.Color; import javafx.scene.control.*; import javafx.event.ActionEvent; import javafx.event.EventHandler; public class Messege_ModalDialog { private Label label = new Label(); private Button btn; private ModalTest parent; public Messege_ModalDialog(String name, String s, final Stage owner_stg, ModalTest mt) { parent = mt; if(s.equals("yes")){ label.setText(name + " , Cool !"); }else{ label.setText(name + " , Bad Boy !"); } btn = new Button("OK"); final TextField tf = new TextField(); final Stage stage = new Stage(); //Initialize the Stage with type of modal stage.initModality(Modality.WINDOW_MODAL); //Set the owner of the Stage stage.initOwner(owner_stg); stage.setTitle("Messege Dialog Stage"); Group root = new Group(); Scene scene = new Scene(root, 380, 250, Color.web("#0000FF", 0.3)); btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { parent.setText_TextArea(tf.getText()); parent.setText_TextArea("OK"); // stage.hide(); stage.close(); } }); tf.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { String s2 = tf.getText(); parent.setText_TextArea(s2); tf.deleteText(0, s2.length()); } }); label.setPrefWidth(200.0); label.setLayoutX((scene.getWidth() -200.0) / 2); btn.setPrefWidth(50.0); btn.setLayoutX((scene.getWidth() - 50.0) / 2 ); label.setLayoutY(scene.getHeight() / 3); btn.setLayoutY(scene.getHeight() / 2); tf.setPrefColumnCount(30); tf.setLayoutX((scene.getWidth() -330.0) / 2 ); tf.setLayoutY(scene.getHeight() / 2 + 50); root.getChildren().addAll(label, btn, tf); stage.setScene(scene); stage.setResizable(false); stage.show(); } }