Last active
December 17, 2015 18:19
-
-
Save aoetk/5652577 to your computer and use it in GitHub Desktop.
JavaFXでダイアログを作成するサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import java.lang.*?> | |
<?import java.util.*?> | |
<?import javafx.geometry.*?> | |
<?import javafx.scene.control.*?> | |
<?import javafx.scene.image.*?> | |
<?import javafx.scene.layout.*?> | |
<?import javafx.scene.paint.*?> | |
<?import javafx.scene.text.*?> | |
<GridPane hgap="14.0" maxHeight="+Infinity" maxWidth="+Infinity" minHeight="-Infinity" minWidth="-Infinity" vgap="20.0" xmlns:fx="http://javafx.com/fxml" fx:controller="aoetk.dialogsample.ConfirmDialogController"> | |
<children> | |
<ImageView fitHeight="60.0" fitWidth="60.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.rowIndex="0" GridPane.valignment="TOP"> | |
<image> | |
<Image url="@confirm.png" /> | |
<!-- place holder --> | |
</image> | |
</ImageView> | |
<VBox alignment="CENTER_LEFT" maxHeight="+Infinity" maxWidth="+Infinity" minHeight="-Infinity" prefWidth="400.0" spacing="7.0" GridPane.columnIndex="1" GridPane.rowIndex="0" GridPane.valignment="CENTER"> | |
<children> | |
<Label fx:id="detailsLabel" text="message" textAlignment="LEFT" wrapText="true"> | |
<font> | |
<Font name="HGPGothicE" size="13.0" /> | |
</font> | |
</Label> | |
</children> | |
</VBox> | |
<HBox maxHeight="-Infinity" maxWidth="+Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="300.0" GridPane.columnIndex="1" GridPane.rowIndex="1"> | |
<children> | |
<HBox id="HBox" alignment="CENTER"> | |
<children> | |
<Button id="btnCancel" cancelButton="true" mnemonicParsing="false" onAction="#handleBtnCancelAction" text="Cancel" HBox.hgrow="NEVER"> | |
<HBox.margin> | |
<Insets right="14.0" /> | |
</HBox.margin> | |
</Button> | |
</children> | |
<HBox.margin> | |
<Insets /> | |
</HBox.margin> | |
</HBox> | |
<Pane maxWidth="+Infinity" HBox.hgrow="ALWAYS" /> | |
<Button id="btnNo" cancelButton="false" minWidth="80.0" mnemonicParsing="false" onAction="#handleBtnNoAction" text="No" HBox.hgrow="NEVER"> | |
<HBox.margin> | |
<Insets /> | |
</HBox.margin> | |
</Button> | |
<HBox id="HBox" alignment="CENTER"> | |
<children> | |
<Button id="btnYes" defaultButton="true" minWidth="80.0" mnemonicParsing="false" onAction="#handleBtnYesAction" text="Yes" HBox.hgrow="NEVER"> | |
<HBox.margin> | |
<Insets left="14.0" /> | |
</HBox.margin> | |
</Button> | |
</children> | |
</HBox> | |
</children> | |
</HBox> | |
</children> | |
<columnConstraints> | |
<ColumnConstraints hgrow="NEVER" maxWidth="-Infinity" minWidth="-Infinity" /> | |
<ColumnConstraints halignment="CENTER" hgrow="ALWAYS" maxWidth="+Infinity" minWidth="-Infinity" /> | |
</columnConstraints> | |
<padding> | |
<Insets bottom="14.0" left="14.0" right="14.0" top="14.0" /> | |
</padding> | |
<rowConstraints> | |
<RowConstraints maxHeight="+Infinity" minHeight="-Infinity" valignment="CENTER" vgrow="ALWAYS" /> | |
<RowConstraints maxHeight="-Infinity" minHeight="-Infinity" vgrow="NEVER" /> | |
</rowConstraints> | |
</GridPane> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aoetk.dialogsample; | |
import java.net.URL; | |
import java.util.ResourceBundle; | |
import javafx.event.ActionEvent; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.Label; | |
import javafx.stage.Window; | |
/** | |
* 確認ダイアログのコントローラー. | |
* | |
* @author aoetk | |
*/ | |
public class ConfirmDialogController implements Initializable { | |
@FXML | |
private Label detailsLabel; | |
private DialogOption selectedOption = DialogOption.CANCEL; | |
@Override | |
public void initialize(URL url, ResourceBundle rb) { | |
} | |
public DialogOption getSelectedOption() { | |
return selectedOption; | |
} | |
public void setMessage(String msg) { | |
detailsLabel.setText(msg); | |
} | |
@FXML | |
void handleBtnYesAction(ActionEvent event) { | |
handleCloseAction(DialogOption.YES); | |
} | |
@FXML | |
void handleBtnNoAction(ActionEvent event) { | |
handleCloseAction(DialogOption.NO); | |
} | |
@FXML | |
void handleBtnCancelAction(ActionEvent event) { | |
handleCloseAction(DialogOption.CANCEL); | |
} | |
private void handleCloseAction(DialogOption selectedOption) { | |
this.selectedOption = selectedOption; | |
getWindow().hide(); | |
} | |
private Window getWindow() { | |
return detailsLabel.getScene().getWindow(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aoetk.dialogsample; | |
/** | |
* ダイアログのオプション定義. | |
* @author aoetk | |
*/ | |
public enum DialogOption { | |
YES, NO, CANCEL; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aoetk.dialogsample; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
/** | |
* ダイアログサンプル. | |
* @author aoetk | |
*/ | |
public class DialogSample extends Application { | |
@Override | |
public void start(Stage stage) throws Exception { | |
Parent root = FXMLLoader.load(getClass().getResource("DialogSampleView.fxml")); | |
Scene scene = new Scene(root); | |
stage.setScene(scene); | |
stage.setTitle("Dialog Sample"); | |
stage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import java.lang.*?> | |
<?import java.util.*?> | |
<?import javafx.scene.*?> | |
<?import javafx.scene.control.*?> | |
<?import javafx.scene.layout.*?> | |
<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml" fx:controller="aoetk.dialogsample.DialogSampleViewController"> | |
<children> | |
<Button fx:id="button" layoutX="97.0" layoutY="84.0" onAction="#handleButtonAction" text="ダイアログを開く" /> | |
</children> | |
</AnchorPane> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aoetk.dialogsample; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.util.ResourceBundle; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javafx.event.ActionEvent; | |
import javafx.fxml.FXML; | |
import javafx.fxml.FXMLLoader; | |
import javafx.fxml.Initializable; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.stage.Modality; | |
import javafx.stage.Stage; | |
import javafx.stage.StageStyle; | |
/** | |
* 初期ウィンドウのコントローラー. | |
* @author aoetk | |
*/ | |
public class DialogSampleViewController implements Initializable { | |
@FXML | |
Button button; | |
@FXML | |
private void handleButtonAction(ActionEvent event) { | |
try { | |
// 確認ダイアログの表示 | |
FXMLLoader loader = new FXMLLoader(getClass().getResource("ConfirmDialog.fxml")); | |
loader.load(); | |
Parent root = loader.getRoot(); | |
ConfirmDialogController controller = loader.getController(); | |
controller.setMessage("今日の外の天気は晴れですか?"); | |
Scene scene = new Scene(root); | |
Stage confirmDialog = new Stage(StageStyle.UTILITY); | |
confirmDialog.setScene(scene); | |
confirmDialog.initOwner(button.getScene().getWindow()); | |
confirmDialog.initModality(Modality.WINDOW_MODAL); | |
confirmDialog.setResizable(false); | |
confirmDialog.setTitle("Select an Option"); | |
confirmDialog.showAndWait(); // ダイアログが閉じるまでブロックされる | |
// 確認ダイアログの選択結果に応じたメッセージダイアログの表示 | |
switch (controller.getSelectedOption()) { | |
case YES: | |
showMessageDialog("コンピュータで遊んでないで外に出よう。\nビーチに行って太陽の日を浴びたらどうでしょう。"); | |
break; | |
case NO: | |
showMessageDialog("屋内にいて様々なものから保護されているのはいいことです。"); | |
break; | |
} | |
} catch (IOException ex) { | |
Logger.getLogger(DialogSampleViewController.class.getName()). | |
log(Level.SEVERE, "読み込み失敗", ex); | |
} | |
} | |
@Override | |
public void initialize(URL url, ResourceBundle rb) { | |
} | |
private void showMessageDialog(String msg) throws IOException { | |
FXMLLoader loader = new FXMLLoader(getClass().getResource("MessageDialog.fxml")); | |
loader.load(); | |
Parent root = loader.getRoot(); | |
MessageDialogController controller = loader.getController(); | |
controller.setMessage(msg); | |
Scene scene = new Scene(root); | |
Stage messageDialog = new Stage(StageStyle.UTILITY); | |
messageDialog.setScene(scene); | |
messageDialog.initOwner(button.getScene().getWindow()); | |
messageDialog.initModality(Modality.WINDOW_MODAL); | |
messageDialog.setResizable(false); | |
messageDialog.setTitle("Message"); | |
messageDialog.show(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import java.lang.*?> | |
<?import java.util.*?> | |
<?import javafx.geometry.*?> | |
<?import javafx.scene.control.*?> | |
<?import javafx.scene.image.*?> | |
<?import javafx.scene.layout.*?> | |
<?import javafx.scene.paint.*?> | |
<?import javafx.scene.text.*?> | |
<GridPane hgap="14.0" maxHeight="+Infinity" maxWidth="+Infinity" minHeight="-Infinity" minWidth="-Infinity" vgap="20.0" xmlns:fx="http://javafx.com/fxml" fx:controller="aoetk.dialogsample.MessageDialogController"> | |
<children> | |
<ImageView fitHeight="60.0" fitWidth="60.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.rowIndex="0" GridPane.valignment="TOP"> | |
<image> | |
<Image url="file:/Users/aoetakashi/NetBeansProjects/DialogSample/src/aoetk/dialogsample/inform.png" /> | |
<!-- place holder --> | |
</image> | |
</ImageView> | |
<VBox alignment="CENTER_LEFT" maxHeight="+Infinity" maxWidth="+Infinity" minHeight="-Infinity" prefWidth="400.0" spacing="7.0" GridPane.columnIndex="1" GridPane.rowIndex="0"> | |
<children> | |
<Label fx:id="detailsLabel" text="message" textAlignment="LEFT" wrapText="true"> | |
<font> | |
<Font name="HGGothicE" size="13.0" /> | |
</font> | |
</Label> | |
</children> | |
</VBox> | |
<HBox maxHeight="-Infinity" maxWidth="+Infinity" minHeight="-Infinity" minWidth="-Infinity" GridPane.columnIndex="1" GridPane.rowIndex="1"> | |
<children> | |
<Pane maxWidth="+Infinity" HBox.hgrow="ALWAYS" /> | |
<HBox id="HBox" alignment="CENTER"> | |
<children> | |
<Button id="okButton" defaultButton="true" minWidth="80.0" mnemonicParsing="false" onAction="#handleBtnOkAction" text="Ok" HBox.hgrow="NEVER"> | |
<HBox.margin> | |
<Insets left="14.0" /> | |
</HBox.margin> | |
</Button> | |
</children> | |
</HBox> | |
</children> | |
</HBox> | |
</children> | |
<columnConstraints> | |
<ColumnConstraints hgrow="NEVER" maxWidth="-Infinity" minWidth="-Infinity" /> | |
<ColumnConstraints halignment="CENTER" hgrow="ALWAYS" maxWidth="+Infinity" minWidth="-Infinity" /> | |
</columnConstraints> | |
<padding> | |
<Insets bottom="14.0" left="14.0" right="14.0" top="14.0" /> | |
</padding> | |
<rowConstraints> | |
<RowConstraints maxHeight="+Infinity" minHeight="-Infinity" valignment="CENTER" vgrow="ALWAYS" /> | |
<RowConstraints maxHeight="-Infinity" minHeight="-Infinity" vgrow="NEVER" /> | |
</rowConstraints> | |
</GridPane> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aoetk.dialogsample; | |
import java.net.URL; | |
import java.util.ResourceBundle; | |
import javafx.event.ActionEvent; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.Label; | |
import javafx.stage.Window; | |
/** | |
* メッセージダイアログのコントローラー. | |
* | |
* @author aoetk | |
*/ | |
public class MessageDialogController implements Initializable { | |
@FXML | |
private Label detailsLabel; | |
@Override | |
public void initialize(URL url, ResourceBundle rb) { | |
} | |
public void setMessage(String msg) { | |
detailsLabel.setText(msg); | |
} | |
@FXML | |
void handleBtnOkAction(ActionEvent event) { | |
getWindow().hide(); | |
} | |
private Window getWindow() { | |
return detailsLabel.getScene().getWindow(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment