How to play a video in a alert using JavaFX? - javafx

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();

Related

JavaFX ContextMenu max size has no effect

If a ContextMenu has lots of items, it fills the entire screen. It seems that ContextMenu.setMaxSize has no effect whatsoever.
Is there a way to restrict the size of a ContextMenu, in a way that it is still scrollable via mouse wheel & and the up and down buttons appear?
I guess I could roll my own control with VBox & Scrollpane, but I'd like to avoid this if possible.
Unfortunately, limiting the size of popup is not supported: the Region that's responsible for showing the MenuItems is ContextMenuContent and implements its computeMaxHeight to return the screenHeight. That container is created by ContextMenuSkin and stored into a private final field, so there's no way to replace it with a custom implementation with a more intelligent implementation.
What we can do, though, is to access that region and set its maxHeight to the same value as the ContextMenu. To remain off the evil illegal reflective access to the private field, we can register a handler for the Menu.ON_SHOWING event and update the size as needed [*].
Something like
public class MaxSizedContextMenu extends ContextMenu {
public MaxSizedContextMenu() {
addEventHandler(Menu.ON_SHOWING, e -> {
Node content = getSkin().getNode();
if (content instanceof Region) {
((Region) content).setMaxHeight(getMaxHeight());
}
});
}
}
[*] update: to make this work, the ContextMenu must have a reasonable maxHeight (default is Double.MAX_VALUE), that is it must be set manually after instantiation. Furthermore, we have to use the ContextMenu's maxHeight in the eventHandler (vs. f.i. an arbitrary constant), otherwise vertical location of the popup is broken - the layout code still thinking, that it's filling the entire screen height.
ContextMenu menu = new MaxSizedContextMenu();
menu.setMaxHeight(200);

JavaFX wait for enter

Essentially, what I'm trying to do is something like to a text-based RPG using JavaFX. Right now, to display some text, I've got this:
final IntegerProperty i = new SimpleIntegerProperty(0);
Timeline timeline = new Timeline();
KeyFrame keyFrame = new KeyFrame(
Duration.millis(70),
event -> {
if (i.get() > info.getText().length()) {
timeline.stop();
} else {
text.setText(info.getText().substring(0, i.get()));
i.set(i.get() + 1);
}
});
timeline.getKeyFrames().add(keyFrame);
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
timeline.setOnFinished(a -> {
hb_start.getChildren().clear();
hb_start.getChildren().addAll(start_left,start_right);
hb_start.setAlignment(Pos.CENTER);
});
Because the length of the animation depends on the size of the text, the cyclecount is set to indefinite. Unless there's some other way I'm missing to make the animation play once and then stop, I'd like it so that when you press enter (or some other key that I decide on later) for it to call timeline.stop(); but I can't figure out how to add any sort of listener. Trying to implement keyListenerseems to come with all sorts of stuff that I don't need, and it also doesn't work with a TextField, and instead wants a JTextField, which might be fine, except that I don't have a clue how to do anything with Swing.
Currently, the text is being displayed in aTextFlow from the text of Text. I'm assuming the listener would be added to the TextFlow, or even the scene itself, honestly, I'm at a loss for what to do. It sounds simple, but I can't seem to figure it out.
KeyListener is a AWT class, not a JavaFX class. Unless you're embedding a Swing component in your JavaFX application or a JavaFX node in a Swing application, you should use JavaFX's equivalent EventHandler<KeyEvent> instead. Furthermore there is no need to include a TextField (or Swing's JTextField) in your application just for the sake of receiving key events. You could add the listener directly to the Scene:
final KeyCode stopKey = KeyCode.ENTER;
EventHandler<KeyEvent> handler = event -> {
if (event.getCode() == stopKey) {
timeline.stop();
}
};
scene.setOnKeyPressed(handler);
Note that events can be consumed by nodes before they reach the scene, e.g. by a TextField that has the focus. In this case you could make sure you get the event by registering a the listener as a event filter instead:
// scene.setOnKeyPressed(handler);
scene.addEventFilter(KeyEvent.KEY_PRESSED, handler);

Check whether there are no tabs in TabPane on close JAVAFX

I tried a few methods:
tabPane.getSelectionModel().selectedItemProperty().addListener((e, o, n)
if (tabPane.getTabs().isEmpty()) someButton.setDisable(false);
and this when creating the tab:
tab.setOnCloseRequest(e ->
if (tabPane.getTabs().isEmpty()) someButton.setDisable(false);
But both are not working. The second method is definitly wrong as it is checking if there are tabs before actually closing the tab. Any solutions? Many thanks
Create isNotEmpty BooleanBinding on TabPane ObservableList<Tab>.
TabPane tp = new TabPane(new Tab("A"),new Tab("B"));
final BooleanBinding empty = Bindings.isNotEmpty(tp.getTabs());
Button someButton = new Button();
someButton.disableProperty().bind(empty);
I'm not sure if the below is what you want, but you can check that the size of the matched tab list in the tabpane <= 1 rather than empty.
tab.setOnCloseRequest(event -> {
TabPane tabPane = tab.getTabPane();
if (tabPane.getTabs().size() <= 1) {
// don't allow the last tab to be closed.
event.consume();
return;
}
});
Consuming the close request will prevent closure, but you could do other work in that event as well or instead (such as manipulating the disable property of your button) if you wish.
Usually button disable properties are well controlled via a binding, so perhaps something like MBec's solution might be a good idea in that case if that is all you need to accomplish.

How do I create a JavaFX Alert with a check box for "Do not ask again"?

I would like to use the standard JavaFX Alert class for a confirmation dialog that includes a check box for "Do not ask again". Is this possible, or do I have to create a custom Dialog from scratch?
I tried using the DialogPane.setExpandableContent() method, but that's not really what I want - this adds a Hide/Show button in the button bar, and the check box appears in the main body of the dialog, whereas I want the check box to appear in the button bar.
Yes, it is possible, with a little bit of work. You can override DialogPane.createDetailsButton() to return any node you want in place of the Hide/Show button. The trick is that you need to reconstruct the Alert after that, because you will have got rid of the standard contents created by the Alert. You also need to fool the DialogPane into thinking there is expanded content so that it shows your checkbox. Here's an example of a factory method to create an Alert with an opt-out check box. The text and action of the check box are customizable.
public static Alert createAlertWithOptOut(AlertType type, String title, String headerText,
String message, String optOutMessage, Consumer<Boolean> optOutAction,
ButtonType... buttonTypes) {
Alert alert = new Alert(type);
// Need to force the alert to layout in order to grab the graphic,
// as we are replacing the dialog pane with a custom pane
alert.getDialogPane().applyCss();
Node graphic = alert.getDialogPane().getGraphic();
// Create a new dialog pane that has a checkbox instead of the hide/show details button
// Use the supplied callback for the action of the checkbox
alert.setDialogPane(new DialogPane() {
#Override
protected Node createDetailsButton() {
CheckBox optOut = new CheckBox();
optOut.setText(optOutMessage);
optOut.setOnAction(e -> optOutAction.accept(optOut.isSelected()));
return optOut;
}
});
alert.getDialogPane().getButtonTypes().addAll(buttonTypes);
alert.getDialogPane().setContentText(message);
// Fool the dialog into thinking there is some expandable content
// a Group won't take up any space if it has no children
alert.getDialogPane().setExpandableContent(new Group());
alert.getDialogPane().setExpanded(true);
// Reset the dialog graphic using the default style
alert.getDialogPane().setGraphic(graphic);
alert.setTitle(title);
alert.setHeaderText(headerText);
return alert;
}
And here is an example of the factory method being used, where prefs is some preference store that saves the user's choice
Alert alert = createAlertWithOptOut(AlertType.CONFIRMATION, "Exit", null,
"Are you sure you wish to exit?", "Do not ask again",
param -> prefs.put(KEY_AUTO_EXIT, param ? "Always" : "Never"), ButtonType.YES, ButtonType.NO);
if (alert.showAndWait().filter(t -> t == ButtonType.YES).isPresent()) {
System.exit();
}
And here's what the dialog looks like:

Create Dialogs with Default Images in JavaFX (Info/Warning/Error)

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

Resources