HeaderBar & Alert/Dialog/DialogPane
Cormac Redmond
credmond at certak.com
Mon Oct 20 22:04:12 UTC 2025
Hi,
I touched on this before, but I still think there are gaps that should be
addressed with using HeaderBars and Alerts/Dialogs/DialogPanes.
There's a bunch of logic in Alert/Dialog/DialogPane regarding headerText
vs. headerNode precedence, graphic node placement, etc., which all depends
on what is set and when, etc.
To incorporate HeaderBar into Dialogs, the advice before was to "get" the
existing header node, if it's there, and set it as the center of a new
BorderPane and stick the HeaderBar as top, and set that BorderPane as the
DialogPane's header, in order to use HeaderBar. But that only partially
solves some problems, some of the time.
If you use a HeaderBar for the purpose I do: to control the color (get rid
of the ugly white bg) & to to save some precious space (i.e., get a menu
system on the left + window icons on the right) *and* want a consistent
look and feel across your application regarding popups, you must sacrifice
all of this "standard" functionality of standard Alerts/Dialogs; *or* don't
use HeaderBar for dialogs -- which looks unpolished and lacking.
Look at this example (which only touches the surface):
public class HeaderBarSupport extends Application {
private boolean INSTALL_HEADER_BAR = true;
@Override
public void start(Stage primaryStage) {
showErrorDialogHeaderText("title", "will see this", "content",
!INSTALL_HEADER_BAR); // No HeaderBar, all is fine
showErrorDialogHeaderText("will not see this title", "will not see
this header text", "content", INSTALL_HEADER_BAR); // Won't see title,
headerText, or big red X
}
private void showErrorDialogHeaderText(String title, String headerText,
String content, boolean installHeaderBar) {
Alert alert = new Alert(Alert.AlertType.ERROR, content); // Expect
error
alert.setTitle(title);
alert.setHeaderText(headerText);
alert.setContentText(content);
if (installHeaderBar) {
installHeaderBar(alert);
}
alert.showAndWait();
}
private void installHeaderBar(final Dialog dialog) {
Node existingHeader = dialog.getDialogPane().getHeader(); // null
in these examples
BorderPane borderPane = new BorderPane();
// Note: could set a label with title in the HeaderBar if desired,
but that's about as much as you can salvage
borderPane.setTop(new HeaderBar());
borderPane.setCenter(existingHeader);
dialog.getDialogPane().setHeader(borderPane);
dialog.initStyle(StageStyle.EXTENDED);
}
public static void main(String[] args) {
launch(args);
}
}
Kind Regards,
Cormac
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/openjfx-dev/attachments/20251020/40462e19/attachment-0001.htm>
More information about the openjfx-dev
mailing list