public class Alert extends Dialog<ButtonType>
Dialogクラスのサブクラスであり、ユーザーにレスポンスを求める場合に簡単に表示できる事前作成済ダイアログ・タイプを数多くサポートしています。 したがって、Alertクラスは、(Dialogを直接使用する場合と比べて、)多くのユーザーのニーズに最も適したクラスです。 また、ユーザーにテキスト入力を求める場合はTextInputDialog、オプション・リストからの選択を求める場合はChoiceDialogを使用する方が適しています。
Alertインスタンスを作成する際、ユーザーはAlert.AlertType列挙値を渡す必要があります。 この値を渡すことにより、Alertインスタンスによって自身が適切に構成されます(title、header、graphicなどの多くのDialogプロパティのデフォルト値と、特定のタイプのダイアログで必要なデフォルトのbuttonsが設定されます)。
Alertを(まだ表示しないが)インスタンス化する場合は、Alert alert = new Alert(AlertType.CONFIRMATION, "Are you sure you want to format your system?");のようなコードを使用します。
Alertがインスタンス化されたら、表示する必要があります。 通常、アラート(およびダイアログ一般)はモーダルなブロッキング形式で表示されます。 'モーダル'とは、ダイアログの表示中はユーザーが所有アプリケーションを操作できないことを意味し、'ブロッキング'とは、ダイアログが表示された時点でコード実行が停止されることを意味します。 つまり、ダイアログを表示し、ユーザー・レスポンスを待機してから、showコールの直後のコードから実行を続行できるため、開発者はダイアログからのユーザー入力にすぐに対応することができます(関連する場合)。
JavaFXダイアログは、デフォルトでモーダルです(これは、Dialog.initModality(javafx.stage.Modality) APIを使用して変更できます)。 ブロッキング・ダイアログまたは非ブロッキング・ダイアログを指定するには、開発者が(それぞれ) Dialog.showAndWait()またはDialog.show()を呼び出すことを選択します。 このような状況でのコーディングを容易にするために、ほとんどの開発者がデフォルトでDialog.showAndWait()を選択する必要があります。 次の3つのコード・スニペットは、前述のように指定されたAlertダイアログを表示するための方法を示したもので、3つとも等しく有効です。
オプション1: '従来型'のアプローチ
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && result.get() == ButtonType.OK) {
formatSystem();
}
オプション2: 従来型+オプションのアプローチ
alert.showAndWait().ifPresent(response -> {
if (response == ButtonType.OK) {
formatSystem();
}
});
オプション3: 完全なラムダ式アプローチ
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent(response -> formatSystem());
上の3つのオプションに優劣はないので、開発者は自分の好きなスタイルで作業できます。 上のオプションを示した目的は、開発者にOptional APIを紹介することにあります。これはJava 8で新しく導入されたものであり、多くの開発者には馴染みがないと思われます。
Dialog、Alert.AlertType、TextInputDialog、ChoiceDialog| Type | プロパティと説明 |
|---|---|
ObjectProperty<Alert.AlertType> |
alertType
Alertインスタンスを作成する際、ユーザーは
Alert.AlertType列挙値を渡す必要があります。 |
contentText, dialogPane, graphic, headerText, height, onCloseRequest, onHidden, onHiding, onShowing, onShown, resizable, resultConverter, result, showing, title, width, x, y| 修飾子と型 | クラスと説明 |
|---|---|
static class |
Alert.AlertType
Alertクラスが様々なプロパティを事前移入するために使用できる、使用可能な事前作成済のアラート・タイプが記述された列挙。 |
| コンストラクタと説明 |
|---|
Alert(Alert.AlertType alertType)
指定されたAlertTypeでアラートを作成します(どれが最も適しているかを調べるには、
Alert.AlertTypeの説明を参照してください)。 |
Alert(Alert.AlertType alertType, String contentText, ButtonType... buttons)
指定されたcontentText、ButtonTypesおよびAlertTypeでアラートを作成します(どれが最も適しているかを調べるには、
Alert.AlertTypeの説明を参照してください)。 |
| 修飾子と型 | メソッドと説明 |
|---|---|
ObjectProperty<Alert.AlertType> |
alertTypeProperty()
Alertインスタンスを作成する際、ユーザーは
Alert.AlertType列挙値を渡す必要があります。 |
Alert.AlertType |
getAlertType()
プロパティalertTypeの値を取得します。
|
ObservableList<ButtonType> |
getButtonTypes()
このAlertインスタンス内部で現在設定されているすべての
ButtonTypeインスタンスのObservableListを返します。 |
void |
setAlertType(Alert.AlertType alertType)
プロパティalertTypeの値を設定します。
|
buildEventDispatchChain, close, contentTextProperty, dialogPaneProperty, getContentText, getDialogPane, getGraphic, getHeaderText, getHeight, getModality, getOnCloseRequest, getOnHidden, getOnHiding, getOnShowing, getOnShown, getOwner, getResult, getResultConverter, getTitle, getWidth, getX, getY, graphicProperty, headerTextProperty, heightProperty, hide, initModality, initOwner, initStyle, isResizable, isShowing, onCloseRequestProperty, onHiddenProperty, onHidingProperty, onShowingProperty, onShownProperty, resizableProperty, resultConverterProperty, resultProperty, setContentText, setDialogPane, setGraphic, setHeaderText, setHeight, setOnCloseRequest, setOnHidden, setOnHiding, setOnShowing, setOnShown, setResizable, setResult, setResultConverter, setTitle, setWidth, setX, setY, show, showAndWait, showingProperty, titleProperty, widthProperty, xProperty, yPropertypublic final ObjectProperty<Alert.AlertType> alertTypeProperty
public Alert(Alert.AlertType alertType)
Alert.AlertTypeの説明を参照してください)。
AlertTypeを渡すことにより、title、headerTextおよびgraphicプロパティのデフォルト値、および導入する関連buttonsが設定されます。 Alertがインスタンス化されたら、開発者は必要に応じてアラートの値を変更できます。
content textプロパティは、デフォルト値が設定されていないために開発者が設定する必要があります(より複雑なアラートを設定しようとする開発者は、alert.getDialogPane().setContent(Node)を呼び出すこともできます)。 contentText (またはcontent)プロパティが設定されていない場合、エンド・ユーザーに有用な情報は表示されません。
public Alert(Alert.AlertType alertType, String contentText, ButtonType... buttons)
Alert.AlertTypeの説明を参照してください)。
ButtonType引数の可変数を渡すことにより、開発者は、ダイアログに表示されるデフォルト・ボタンを直接オーバーライドして、事前定義済のボタンを可変引数配列で指定されたボタンで置換します。
AlertTypeを渡すことにより、title、headerTextおよびgraphicプロパティのデフォルト値が設定されます。 Alertがインスタンス化されたら、開発者は必要に応じてアラートの値を変更できます。
public final Alert.AlertType getAlertType()
public final void setAlertType(Alert.AlertType alertType)
public final ObjectProperty<Alert.AlertType> alertTypeProperty()
public final ObservableList<ButtonType> getButtonTypes()
ButtonTypeインスタンスのObservableListを返します。 ButtonTypeは、事前定義済のタイプの1つ(ButtonType.OKなど)にすることも、(ButtonType.ButtonType(String)またはButtonType.ButtonType(String, javafx.scene.control.ButtonBar.ButtonData)コンストラクタを介して作成した)カスタム・タイプにすることもできます。
詳細は、ButtonTypeクラスの説明を参照してください。ただし、高いレベルでは、各ButtonTypeインスタンスはNodeに変換されます、(一般的には、DialogPaneで(オーバーライド可能な) DialogPane.createButton(ButtonType)を介してButtonに変換されます)。
Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.