JavaFxでダイアログ(メッセージ・フォルダ・ファイル選択)を表示する方法調べた。
参考URL)JavaFX メッセージ・ダイアログ
ダイアログを表示するユーティリティクラスを作成してみた。
(staticメソッドなので、どこからでも呼べるように作成。)
DialogUtil.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
package util; import java.io.File; import java.util.Optional; import application.Main; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.ButtonType; import javafx.stage.DirectoryChooser; import javafx.stage.FileChooser; import javafx.stage.FileChooser.ExtensionFilter; /** * ダイアログに関するユーティリティクラス. */ public final class DialogUtil { /** * コンストラクタ. */ private DialogUtil() { } /** * メッセージダイアログを表示します. * @param alertType アラートタイプ * @param message メッセージ * @return ボタンタイプ */ public static ButtonType show(final AlertType alertType, final String message) { Alert alert = new Alert(alertType); // 確認の場合(はい/いいえ) if (alertType == AlertType.CONFIRMATION) { alert = new Alert(alertType, "", ButtonType.YES, ButtonType.NO); } alert.setTitle(getTitle(alertType)); alert.setHeaderText(null); alert.setContentText(message); Optional<ButtonType> optional = alert.showAndWait(); // ×ボタンの場合にemptyの場合あるので、CANCELを設定 if (Optional.empty().equals(optional)) { return ButtonType.CANCEL; } return optional.get(); } /** * タイトルの取得. * @param alertType アラートタイプ * @return タイトル */ private static String getTitle(final AlertType alertType) { String title = ""; if (AlertType.CONFIRMATION == alertType) { title = "確認"; } else if (AlertType.INFORMATION == alertType) { title = "情報"; } else if (AlertType.WARNING == alertType) { title = "警告"; } else if (AlertType.ERROR == alertType) { title = "エラー"; } return title; } /** * ディレクトリ選択ダイアログを表示します. * @param title タイトル * @return ディレクトリパス. */ public static String showSelectDir(final String title) { DirectoryChooser fc = new DirectoryChooser(); fc.setTitle(title); // 初期で開くディレクトリパスを指定 fc.setInitialDirectory(new File(getCurrentDirPath())); // ダイアログを表示 ※引数にメインのstageを指定する File importFile = fc.showDialog(Main.getInstance().getMainStage()); String selectDirPath = null; if (importFile != null) { selectDirPath = importFile.getPath().toString(); } System.out.println(String.format("[%s]選択ディレクトリパス = %s", title, selectDirPath)); return selectDirPath; } /** * ファイル選択ダイアログを表示します. * @param title タイトル * @return ファイルパス */ public static String showSelectFile(final String title) { final FileChooser fc = new FileChooser(); fc.setTitle(title); // 選択できる拡張子を選択 for (ExtensionEnum extension : ExtensionEnum.values()) { fc.getExtensionFilters().add(getExtensionFilter(extension)); } // 初期で開くディレクトリパスを指定 fc.setInitialDirectory(new File(getCurrentDirPath())); // ダイアログを表示 ※引数にメインのstageを指定する File importFile = fc.showOpenDialog(Main.getInstance().getMainStage()); String selectFilePath = null; if (importFile != null) { selectFilePath = importFile.getPath().toString(); } System.out.println(String.format("[%s]選択ファイルパス = %s", title, selectFilePath)); return selectFilePath; } /** * カレントディレクトリパスを取得します. * @return カレントディレクトリパス */ private static String getCurrentDirPath() { return new File("").getAbsolutePath(); } /** * 拡張子フィルタークラスを取得します. * @param extension 拡張子Enum * @return 拡張子フィルタークラス */ private static ExtensionFilter getExtensionFilter(final ExtensionEnum extension) { return new FileChooser.ExtensionFilter(extension.getName(), extension.getExtensions()); } /** * ファイル拡張子の列挙体. */ public enum ExtensionEnum { /** Excel. */ Excel("Excel", "xlsx", "*.xlsx"), /** Csv. */ Csv("Csv", "csv", "*.csv"); /** 名称. */ private String name = null; /** 拡張子名称. */ private String extensionName = null; /** 拡張子. */ private String extensions = null; /** * コンストラクタ. * @param arg1 名称 * @param arg2 拡張子名称 * @param arg3 拡張子 */ ExtensionEnum(final String arg1, final String arg2, final String arg3) { this.name = arg1; this.extensionName = arg2; this.extensions = arg3; } /** * 名称を取得します. * @return 名称 */ public String getName() { return this.name; } /** * 拡張子名称を取得します. * @return 拡張子名称 */ public String getExtensionName() { return this.extensionName; } /** * 拡張子を取得します. * @return 拡張子 */ public String getExtensions() { return this.extensions; } } } |
以上です。