2015年4月13日 星期一

JavaFX Dialog (3)

除了Alert類別之外,另外尚有ChoiceDialogTextInputDialog類別,皆繼承自Dialog類別,分別在對話盒中顯示選項方塊 (Choice Box) 與文字欄位 (Text Field),此外亦可以Dialog類別自訂對話盒。

以下範例示範以TextInputDialog建立文字欄位之對話盒,較之前以getDialogPane()setContent()setExpandableContent()方法將自訂物件置於對話盒的方式為簡單:
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("Text Input Dialog");
dialog.setHeaderText(null);
dialog.setContentText("Username:");

dialog.showAndWait().ifPresent(result -> {
  System.out.println("Username: " + result);
});     
...
【執行結果】
以下範例並加入setHeaderText()方法設定標題文字:
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("Text Input Dialog");
dialog.setHeaderText("示範以setHeaderText()方法設定標題文字。");
dialog.setContentText("Username:");

dialog.showAndWait().ifPresent(result -> {
  System.out.println("Username: " + result);
});     
...
【執行結果】
以下範例示範以ChoiceDialog建立選項方塊之對話盒,並示範將作業系統的所有字型置於選項方塊之中:
// 取得作業系統的所有字型
List<String> fonts = Font.getFamilies();

// 以字型建立選項方塊對話盒
ChoiceDialog<String> dialog = 
  new ChoiceDialog<>(fonts.get(0), fonts);
dialog.setTitle("Choice Dialog");
dialog.setHeaderText(null);
dialog.setContentText("Fonts:");

dialog.showAndWait().ifPresent(result -> {
  System.out.println("Font: " + result);
});     
...
【執行結果】
除此之外,亦可以Dialog類別自訂對話盒,請參考以下範例,分別加入文字欄位、修改對話盒圖示與按鈕類型組合:
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Custom Dialog");
dialog.setHeaderText(null);
dialog.setGraphic(new ImageView(
  new Image(getClass().getResourceAsStream("images/info.png"))));

// 自訂按鈕類型組合
ButtonType button1 = new ButtonType("OK", ButtonData.OK_DONE);
ButtonType button2 = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);

dialog.getDialogPane().getButtonTypes().addAll(button1, button2);

Label label1 = new Label("Username:");
Label label2 = new Label("Password:");

final TextField textfield = new TextField();
textfield.setText("");
textfield.setTooltip(new Tooltip("Username"));

final PasswordField passwordfield = new PasswordField();
passwordfield.setText("Athena");
passwordfield.setPrefColumnCount(15);
passwordfield.setPromptText("Please enter your password.");
passwordfield.setTooltip(new Tooltip("Password"));

GridPane gridpane = new GridPane();
gridpane.setAlignment(Pos.CENTER);
gridpane.setHgap(10);
gridpane.setVgap(10);
gridpane.setPadding(new Insets(5, 5, 5, 5));

gridpane.add(label1, 0, 0);
gridpane.add(label2, 0, 1);

gridpane.add(textfield, 1, 0);
gridpane.add(passwordfield, 1, 1);

Button btnOK = (Button) dialog.getDialogPane().lookupButton(button1);
btnOK.setDisable(true);

textfield.textProperty().addListener(
  (observable, oldValue, newValue) -> {
  
  btnOK.setDisable(newValue.trim().isEmpty());
});

// 將自訂物件置於對話盒中
dialog.getDialogPane().setContent(gridpane);

dialog.setResultConverter(buttontype -> {
  if (buttontype == button1) {
    return new Pair<>(textfield.getText(), passwordfield.getText());
  }
  return null;
});

dialog.showAndWait().ifPresent(result -> {
  System.out.println("Username: " + result.getKey());
  System.out.println("Password: " + result.getValue());
});
...
【執行結果】
【參考資料】

[1] Java Official Web Site:http://www.oracle.com/technetwork/java/index.html
[2] JavaFX:http://www.oracle.com/technetwork/java/javafx
[3] JavaFX 8.0 API Specification.
[4] Java Platform, Standard Edition 8 API Specification.

© Chia-Hui Huang

2 則留言:

  1. 請教一個讓小弟疑惑許久的問題:
    Java Swing可以new出新視窗,但是…JavaFX不是用new的吧?
    官方有一個登入的dialog視窗,可是沒有登入後的視窗呢…

    回覆刪除
    回覆
    1. 請參考以下文章

      http://leohkkimo.blogspot.tw/2014/11/javafx.html

      刪除