JavaFX には標準で Dialog Box が無い!
なんで無いのかは解らないけど JavaFX 8 なら外部ライブラリーの ControlsFX の Dialog が使えるようです。
ちょっと試してみました。
リストから選択された Dialog を表示させて Dialog での処理をテキストエリアに表示させるだけのシンプルなプログラムです。
こんな感じで動きました。
かなり良い感じの Dialog Box になってます。
ただ、Font Dialog は標準出力に勝手に表示されます。デバッグ用のが残ってるんでしょうか?
それと気になるのは Choice Input Dialog でデフォルトを設定した場合、デフォルト選択のまま Ok ボタンを押すと null が返されるのは何故なんだろう?
私的に少し馴染めない仕様になってるところがあるのでちょっと注意が必要です。
それでは Java 8 環境が必要ですがこのプログラムのコードと Java Web Start を貼っておきますね!
package jp.yucchi.trycontrolsfx_dialog;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Yucchi
*/
public class TryControlsFX_Dialog extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setTitle("ControlsFX の Dialog Box");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package jp.yucchi.trycontrolsfx_dialog;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.control.TextArea;
import javafx.scene.text.Font;
import org.controlsfx.control.action.Action;
import org.controlsfx.dialog.Dialogs;
import org.controlsfx.dialog.Dialogs.CommandLink;
/**
*
* @author Yucchi
*/
public class FXMLDocumentController implements Initializable {
@FXML
private ListView listView;
@FXML
private TextArea textArea;
private Font font;
@Override
public void initialize(URL url, ResourceBundle rb) {
MultipleSelectionModel model = listView.getSelectionModel();
model.selectedItemProperty().addListener((ObservableValue extends String> value, String oldValuet, String newValue) -> {
textArea.appendText(oldValuet + " to " + newValue + "\n");
if (model.getSelectedItems().get(0).toString() != null) {
switch (model.getSelectedItems().get(0).toString()) {
case "Information":
Dialogs.create()
.owner(listView.getScene().getWindow())
.title("JavaFX")
.masthead(isMastheadVisible() ? "ControlsFX" : null)
.message("ControlsFX による Information Dialog Box です。")
.showInformation();
textArea.appendText("OK\n\n"); // 戻り値無し
break;
case "Confirmation":
Action response = Dialogs.create()
.owner(listView.getScene().getWindow())
.title("Confirmation")
.masthead(isMastheadVisible() ? "あなたの好みは?" : null)
.message("あなたは巨乳が好きですか?")
.showConfirm();
textArea.appendText(response.toString() + "\n\n");
break;
case "Warning":
Action responseWarning = Dialogs.create()
.owner(listView.getScene().getWindow())
.title("Warning")
.masthead(isMastheadVisible() ? "やる気がなくなってきました。" : null)
.message("Ok ボタンをクリックしてやる気を補充してください。")
.showWarning();
textArea.appendText(responseWarning.toString() + "\n\n");
break;
case "Error":
Action responseError = Dialogs.create()
.owner(listView.getScene().getWindow())
.title("Error")
.masthead(isMastheadVisible() ? "エラーです。" : null)
.message("小さいことは気にせず、おおらかな心で対処しましょう!")
.showError();
textArea.appendText(responseError.toString() + "\n\n");
break;
case "Exception":
Action responseException = null;
try {
responseException = Dialogs.create()
.owner(listView.getScene().getWindow())
.title("Exception")
.masthead(isMastheadVisible() ? "例外発生しました。" : null)
.message("気になるならスタックトレースでもご覧になってください。")
.showException(Exception.class.newInstance());
} catch (InstantiationException | IllegalAccessException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
textArea.appendText(responseException.toString() + "\n\n");
break;
case "Text Input":
String responseTextInput = Dialogs.create()
.owner(listView.getScene().getWindow())
.title("Text Input")
.masthead(isMastheadVisible() ? "あなたのお名前を入力してね♡" : null)
.message("あなたのお名前は?")
.showTextInput();
textArea.appendText(responseTextInput + "\n\n");
break;
case "Choice Input":
List items = Arrays.asList("Java", "Haskell", "C++", "PHP", "日本語");
String responseChoiceInput = Dialogs.create()
.owner(listView.getScene().getWindow())
.title("Coice Input")
.masthead(isMastheadVisible() ? "あなたの好みは?" : null)
.message("あなたの好きな言語を選んでください。")
// .showChoices(items.get(0), items); // デフォルトを選択状態で Ok だとぬるぽだよ。
.showChoices(items); // 何も選択しない状態で Ok だとぬるぽ。(これは解る)
textArea.appendText(responseChoiceInput + "\n\n");
break;
case "Command Link":
List links = Arrays.asList(
new CommandLink("プログラムを終了します。",
"未保存のデータは破棄されます。\n*:..。o○☆*゜(・ェ・人) .*:..。o○☆*゜ゴメンЙЁ!! "),
new CommandLink("プログラムの応答をまちます。",
"いつ終了するか解りませんのでゆっくりお茶でもしばいてください。\n(。・・)_且~~ お茶どうぞ "),
new CommandLink("コンピューターを窓から投げ捨てます。",
"ヽ(#`Д´)ノこんなのいらん! \n(ノToT)ノ ┫:・'.::・┻┻:・'.::・ "));
Action responseCommandLinks = Dialogs.create()
.owner(listView.getScene().getWindow())
.title("Command Link")
.masthead(isMastheadVisible() ? "Windows が応答しなくなった!" : null)
.message("Windows が仕事放棄しました。\nいつものことですがどうされますか?")
.showCommandLinks(links.get(1), links);
textArea.appendText(responseCommandLinks.toString() + "\n\n");
break;
case "Font Selector":
Font responseFontSelector = Dialogs.create()
.owner(listView.getScene().getWindow())
.title("Font Selector")
.masthead(isMastheadVisible() ? "フォントを設定します。" : null)
.message("あなたの好みのフォントを選んでください。")
.showFontSelector(font);
textArea.appendText(responseFontSelector.toString() + "\n\n");
break;
}
} else {
textArea.appendText("model: null\n\n");
}
});
}
private boolean isMastheadVisible() {
return true;
}
}
Webstart: click to launch this app as webstart (Java 8 実行環境必須)
Hatena タグ: JavaFX
I read a lot of interesting articles here. Probably you spend a lot of time writing, i know how to
save you a lot of time, there is an online tool that creates
unique, SEO friendly articles in seconds, just type in google
– masagaltas free content