DialogPane
DialogPane is the main container used inside a dialog window in JavaFX. It controls:
- the message/content
- the header
- the buttons at the bottom
- optional graphics/icons
Usually you do not create a DialogPane directly. Instead, it is automatically used by classes like:
AlertDialogTextInputDialog
Example with Alert:
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information");
alert.setHeaderText("Download Complete");
alert.setContentText("The file was saved successfully.");
alert.showAndWait();
Internally, that alert contains a DialogPane.
You can access and customize it:
DialogPane pane = alert.getDialogPane();
Example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
public class DialogPaneExample extends Application {
@Override
public void start(Stage stage) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Delete File");
alert.setHeaderText("Are you sure?");
alert.setContentText("This action cannot be undone.");
DialogPane pane = alert.getDialogPane();
// Change minimum size
pane.setMinHeight(Region.USE_PREF_SIZE);
// Add custom CSS style
pane.setStyle("-fx-font-size: 14px;");
alert.showAndWait().ifPresent(result -> {
if (result == ButtonType.OK) {
System.out.println("User clicked OK");
}
});
}
public static void main(String[] args) {
launch();
}
}
Main parts of a DialogPane:
pane.setHeaderText("Header");
pane.setContentText("Message");
pane.getButtonTypes().add(ButtonType.OK);
You can also add your own custom content:
TextField nameField = new TextField();
DialogPane pane = new DialogPane();
pane.setHeaderText("Enter your name");
pane.setContent(nameField);
Example using Dialog directly:
Dialog<String> dialog = new Dialog<>();
dialog.setTitle("User Input");
DialogPane pane = dialog.getDialogPane();
pane.setHeaderText("Enter your username");
TextField textField = new TextField();
pane.setContent(textField);
pane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
dialog.setResultConverter(button -> {
if (button == ButtonType.OK) {
return textField.getText();
}
return null;
});
dialog.showAndWait().ifPresent(name ->
System.out.println("Hello " + name));
FXML example:
<DialogPane xmlns:fx="http://javafx.com/fxml">
<headerText>
Example Header
</headerText>
<content>
<Label text="This is inside the dialog"/>
</content>
<buttonTypes>
<ButtonType fx:constant="OK"/>
<ButtonType fx:constant="CANCEL"/>
</buttonTypes>
</DialogPane>
Important methods:
pane.setHeaderText(...);
pane.setContentText(...);
pane.setContent(node);
pane.getButtonTypes();
pane.lookupButton(ButtonType.OK);
lookupButton(...) lets you get the actual button and modify it:
Button okButton = (Button) pane.lookupButton(ButtonType.OK);
okButton.setDisable(true);
This is useful if you want the OK button disabled until the user enters valid text.