I am creating a JavaFX Dialog and want to use the default icons for Info/Warning/Error.
In Swing, I can get the Information icon this way:
UIManager.getIcon("OptionPane.informationIcon")
How can I do the same in JavaFX?
I asked for this some days ago. I use this code to make labels with default icons:
Label img = new Label();
img.getStyleClass().addAll("alert", "error", "dialog-pane");
dialog.setGraphic(img);
If you get a copy of the images you can use these ideas.
Alert example:
#FXML void handleHelpButton(ActionEvent event){
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Help");
alert.setHeaderText("Help");
alert.setGraphic(new ImageView(this.getClass().getResource("img/help.png").toString()));
alert.setContentText("Place the cursor over a button for hint.");
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(this.getClass().getResource("img/help.png").toString()));
alert.showAndWait();
}
Dialog example:
ChoiceDialog<String> dialog = new ChoiceDialog<>(currentFullscreenSetting, choices);
dialog.setTitle("Settings");
dialog.setHeaderText("Settings");
dialog.setContentText("Fullscreen on startup: ");
dialog.setGraphic(new ImageView(this.getClass().getResource("img/settings.png").toString()));
Stage stage2 = (Stage) dialog.getDialogPane().getScene().getWindow();
stage2.getIcons().add(new Image(this.getClass().getResource("img/settings.png").toString()));
// Traditional way to get the response value.
Optional<String> result = dialog.showAndWait();
This part of both examples is tricky. I noticed that this would not work unless I had the image in the same folder as the .fxml and controller.java files or in a folder that is in the same folder has the files mentioned. You might have to play with you file location. In my example it appears that my setGraphic and getIcons images are in the same folder, but they are not.
stage.getIcons().add(new
Image(this.getClass().getResource("img/help.png").toString()));
My file structure looks like:
PlanningChart
css
img
planningchart
img
The second img folder holds the images for stage.getIcons.add(). The images could also be probably use to for setGraphic. I did not try it.
You do not need to create custom JavaFX dialogs for Info/Warning/Error, since JavaFX already have created Alerts for you.
The Alert class subclasses the Dialog class, and provides support for a number of pre-built dialog types that can be easily shown to users to prompt for a response.
You can create different types of Alerts, depending on the AlertType, it will embed the necessary image.
For Information alert use :
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText("An Information Dialog");
alert.setContentText("Information Message");
alert.showAndWait();
Similarly, for Warning alert, you can use
AlertType.WARNING
and for Error alert, you can use :
AlertType.ERROR
Related
I'm trying to play a video in a alert dialog using JavaFX. The problem is that I can't find how to display the video or more how to insert it in the alert ?
Here is my alert code
MediaPlayer player = new MediaPlayer( new Media(getClass().getResource("video.mp4").toExternalForm()));
MediaView mediaView = new MediaView(player);
private void alert(){
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Don't be a fool");
alert.setHeaderText("");
alert.setContentText("Do you really think your time is correct ?");
Optional<ButtonType> result = alert.showAndWait();
}
An Alert extends from Dialog, which means you can customize its DialogPane. If you want to add a video to your alert, the best place is probably the dialog pane's content. But note that setting the content will replace the contentText (which you set in your example code):
In addition to the header and content properties, there exists header text and content text properties. The way the *Text properties work is that they are a lower precedence compared to the Node properties, but they are far more convenient for developers in the common case, as it is likely the case that a developer more often than not simply wants to set a string value into the header or content areas of the DialogPane.
This means, if you still want to display "Do you really think your time is correct?", you'll have to add your own Label to the content as well. For example:
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Don't be a fool");
alert.setHeaderText("");
Label label = new Label("Do you really think your time is correct?");
VBox content = new VBox(10, label, mediaView);
content.setAlignment(Pos.CENTER);
alert.getDialogPane().setContent(content);
alert.setOnShowing(e -> player.play());
alert.showAndWait();
I am trying my way through JavaFX and still have many - probably silly - beginner questions.
My problem of the day is the following:
I am creating, in Scene builder and Controller, a FlowPane to which I want to add a right-click option, that opens a Context Menu.
Through the scene builder I have added the function OnContextMenuRequested and defined it in the Controller.
To check, I have added a print commend and a Dialog Box to the function, which work well.
Yet, the Context Menu does not work..
Anybody could help and tell me what am I missing???
Thanks in advance...
public void contextMenu(ContextMenuEvent contextMenuEvent) {
// working fine ..
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information");
alert.setHeaderText("Look");
alert.setContentText("Message");
alert.showAndWait();
// working fine
System.out.println("Hello");
// Context Menu ......... not working
ContextMenu contextMenu = new ContextMenu();
MenuItem quit = new MenuItem("quit");
MenuItem hello = new MenuItem("hello");
contextMenu.getItems().addAll(quit, hello);
contextMenu.setX(10.0);
contextMenu.setY(10.0);
contextMenu.show();
????.setContextMenu(????)
}
Unless you have a control, you need to show the ContextMenu "manually" using one of the methods defined in ContextMenu:
// contextMenu.setX(10.0);
// contextMenu.setY(10.0);
contextMenu.show((Node) contextMenuEvent.getSource(), contextMenuEvent.getScreenX(), contextMenuEvent.getScreenY());
Are AX dialog buttons limited to OK and Cancel?
Is it possible to add a custom button to the dialog?
I have the following code for my dialog:
static void mitTabPage(Args _args)
{
Dialog dialog;
DialogGroup dialoggroup, dialoggroup2;
DialogField dialogfield, dialogfield2;
;
dialog = new Dialog ("A new Dialog");
dialog.addTabPage("Brand Id's");
dialoggroup = dialog.addGroup("Brand Id's");
dialogfield = dialog.addField(extendedTypeStr(SYCCarBrandId));
dialog.addTabPage("Owners");
dialoggroup2 = dialog.addGroup("Owners");
dialogfield2 = dialog.addField(extendedTypeStr(SYCOwner));
dialog.run();
}
I'd like to add another button to the dialog. How can I do that?
The Dialog framework is a simple framework for prompting users to obtain some data/settings then performing some action or canceling.
For what you're trying to do, it most likely doesn't make sense to use the dialog framework and instead you could/should create another form if you need additional functionality.
However, if you do insist on using the Dialog framework for this, you would add a runtime button and use registerOverrideMethod.
See following links:
https://msdn.microsoft.com/en-us/library/dialogfield.registeroverridemethod.aspx
https://blogs.msdn.microsoft.com/axsupport/2015/06/07/using-x-to-add-a-control-at-runtime/
I have two layouts in JavaFX app. The first one contains table view whereas the second one is a simple dialog to input data. The problem is that I want to refresh data after closing the dialog. Now I have a button on the first layout which refreshes data:
data.removeAll(data);
loadDataToTable();
But I don't want to invoke methods shown above with the button but automatically right after closing the dialog. I don't know how to make this, let's say, connection between those controllers.
thanks in advance
The new Dialog, if I am not wrong must be a new Stage ! Let us consider the new Stage to be modifyStage. We can call the onSetCloseRequest of the new Stage and put your code in it.
modifyStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent paramT) {
data.removeAll(data);
loadDataToTable();
}
});
I am creating a sample code to show the Stage designed in Javafx, It should not have Minimize and Maximize Button only Close ('X') button required.
For that we are using following code.
Stage stage = new Stage();
// Here we have load it using JFXML
stage.initModality(Modality.WINDOW_MODAL);
stage.initStyle(StageStyle.UTILITY);
stage.setResizable(true);
if (title != null && !title.trim().isEmpty()) {
stage.setTitle(title);
}
stage.setWidth(w);
stage.setHeight(h);
stage.getIcons().add(new Image(Dialog.class.getResourceAsStream("/image/myicon.png")));
stage.showAndWait();
Now the icon I set on the stage is not visible.
What I am missing ?
I assume Windows as the OS (in MacOSX the icon is shown). Under Windows, StageStyle.UTILITY leads to using WS_EX_TOOLWINDOW in which case the icon is not shown. You probably need to use another StageStyle.
"PRB: WS_EX_TOOLWINDOW Windows Do Not Show System Menu Icon": http://support.microsoft.com/kb/179376