Add text under buttons in Dialog - javafx

I was wondering if there is a way to display some text (like a info) under the buttons of a Dialog ? I've looked in many places, but even to align the buttons seems messy (from this post).
This is what I got for now. I just want the "Set my choice ..." text under the 2 buttons.
I looked for a function in the documentation that could help me display the way I want (like "getButtonBar()" or something like that) with no chance. Also creating a new ButtonBar seems a bit complicated for what I want to achieve.
I also tried to create a stage that could look like a dialog, but I needed the result incoming from clicking "Yes / No" in the same way the Dialogs do.
Is there any way to achieve want I want ? Or do I have to build it completely myself ? Thanks !

Just override the createButtonBar() method of DialogPane:
DialogPane pane = new DialogPane() {
#Override
public Node createButtonBar() {
VBox vbox = new VBox(5);
vbox.setAlignment(Pos.BOTTOM_RIGHT);
vbox.setPadding(new Insets(5));
vbox.getChildren().add(super.createButtonBar());
vbox.getChildren().add(new Label("Additional text"));
return vbox ;
}
};
Here's a SSCCE:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class CustomDialogPaneTest extends Application {
#Override
public void start(Stage primaryStage) {
Button button = new Button("Show Dialog");
button.setOnAction(e -> {
DialogPane pane = new DialogPane() {
#Override
public Node createButtonBar() {
VBox vbox = new VBox(5);
vbox.setAlignment(Pos.BOTTOM_RIGHT);
vbox.setPadding(new Insets(5));
vbox.getChildren().add(super.createButtonBar());
vbox.getChildren().add(new Label("Additional text"));
return vbox ;
}
};
CheckBox checkBox = new CheckBox("A check box");
pane.setContent(checkBox);
pane.setHeaderText("The header");
pane.getButtonTypes().addAll(ButtonType.YES, ButtonType.NO);
Dialog<ButtonType> dialog = new Dialog<>();
dialog.setDialogPane(pane);
dialog.showAndWait().ifPresent(System.out::println);
});
StackPane root = new StackPane(button);
root.setPadding(new Insets(20));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Related

How to use modal dialog in this case?

I have a question. I need to make a GridPane with a directory choose that will then lead me to a modal dialog showing photos. I cannot figure how to do the modal dialog that also has to be a GridPane or a HBox...so the question is , how do I get to show a Modal Dialog after selecting the Folder and pressing the "Show" Button... Thanks a lot!
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
public class FotoView extends Application {
#Override
public void start(Stage primaryStage) {
TextField tf = new TextField();
Button b1 = new Button("Search");
Button b2 = new Button("Show");
DirectoryChooser dc = new DirectoryChooser();
GridPane gp = new GridPane();
gp.add(tf, 0 , 0);
gp.add(b1, 1, 0);
gp.add(b2, 0, 1);
b1.setOnAction(e-> dc.showDialog(primaryStage));
primaryStage.setScene(new Scene(gp)) ;
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
} ```
Below is a quick example where a first window has a button that opens up a DirectoryChooser. Once a directory has been selected a second smaller window opens up with the Modality set to APPLICATION_MODAL. In this second window you could add the image(s) that you load and add them to the GridPane.
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(final String[] args) {
launch(args);
}
#Override
public void start(final Stage aStage) throws Exception {
final HBox root = new HBox();
final Button browseBtn = new Button("Click to open a Directory chooser");
root.getChildren().add(browseBtn);
browseBtn.setOnAction(e -> {
final DirectoryChooser chooser = new DirectoryChooser();
final File dir = chooser.showDialog(aStage);
openNewModalStage(aStage, dir);
});
final Scene scene = new Scene(root, 500, 500);
aStage.setScene(scene);
aStage.show();
}
private void openNewModalStage(final Stage aStage, final File aDirectory) {
final Stage stage = new Stage();
final GridPane grid = new GridPane();
final Scene scene = new Scene(grid);
grid.setStyle("-fx-background-color:black");
grid.setPrefWidth(400);
grid.setPrefHeight(400);
// get your images from 'aDirectory' and add them to your grid pane.
stage.setScene(scene);
// set the new windows Modality.
stage.initModality(Modality.APPLICATION_MODAL);
stage.show();
}
}
This way you would only need the one button and the dialog would show as soon as you've selected a directory. However, if you would still want a Search and Show button then just store the directory as a variable and add a listener on the 'show' button and move the openNewModalStage call to that one and remove the second argument.
Edit:
Also, depending on how many images and exactly what you want to display in the modal window, you might want to reconsider the GridPane and use a TilePane, or an hbox/vbox inside of a scroll pane. It's just a thought but I don't know what you will be doing with the GridPane.

Javafx custom cursor reverts to system when hovering over background

I have added a default cursor:
someScene.setCursor(getSomeCursor());
This is working. I have this cursor-code working everywhere in my application, and it looks fine. But whenever I hover over a background GUI component, even if that background component, (be that a VBox, a Canvas or whatever), uses the correct cursor itself, the cursor reverts to the system cursor, instead of my custom cursor.
Any help appreciated.
Here is a complete worked example, which exactly produces the problem: the http link to that "blue cursor" was live at the time of posting. But please check if you get a problem reproducing.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.ImageCursor;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class CursorTest extends Application {
#Override
public void start(Stage stage) {
SomeDialog someDialog = new SomeDialog(stage);
GridPane root = new GridPane();
HBox buttonHolder= new HBox();
buttonHolder.setPrefSize(300,300);
Scene theScene = new Scene(root);
Canvas canvas = new Canvas();
canvas.setWidth(300);
canvas.setHeight(300);
canvas.getGraphicsContext2D().setFill(Color.RED);
canvas.getGraphicsContext2D().fillRoundRect(100, 100, 100, 100, 10, 10);
Image cursorImg = new Image("http://www.pngmart.com/files/3/Cursor-Arrow-PNG-Transparent-Image.png");
ImageCursor imageCursor = new ImageCursor(cursorImg);
theScene.setCursor(imageCursor);
stage.setTitle("Ribbon dialog");
stage.setScene(theScene);
VBox vBox = new VBox();
vBox.getChildren().add(canvas);
vBox.setSpacing(50);
Button button = new Button("Show modal");
button.setPadding(new Insets(30,30,30,30));
button.setOnMouseClicked(event -> {
someDialog.showMeAndWait();
});
buttonHolder.getChildren().add(button);
root.getChildren().addAll(vBox, buttonHolder);
stage.show();
}
class SomeDialog {
Stage newStage;
public SomeDialog(Stage primaryStage) {
newStage = new Stage();
GridPane gridPane = new GridPane();
Scene theScene = new Scene(gridPane);
Image cursorImg = new Image("http://www.pngmart.com/files/3/Cursor-Arrow-PNG-Transparent-Image.png");
ImageCursor imageCursor2 = new ImageCursor(cursorImg);
theScene.setCursor(imageCursor2);
theScene.setCursor(imageCursor2);
theScene.setFill(Color.TRANSPARENT);
newStage.initOwner(primaryStage);
newStage.initModality(Modality.APPLICATION_MODAL);
newStage.setTitle("Player inventory");
newStage.setScene(theScene);
newStage.initStyle(StageStyle.TRANSPARENT);
VBox vBox = new VBox();
Button someBtn = new Button("Close");
someBtn.setPadding(new Insets(100,100,100,100));
someBtn.setOnMouseClicked(event -> newStage.close());
vBox.getChildren().add(someBtn);
gridPane.getChildren().add(vBox);
}
public void showMeAndWait() {
newStage.showAndWait();
}
}
}

Using JavaFX: Is It Possible to Put A GridPane in an Alert Dialog Box?

I have an alert box that is populated with text and for formatting reasons I was wondering if I could somehow put a GridPane inside this Alert box so all the text is spaced out correctly.
If it is not possible to do this with GridPane is there some other way of formatting text I could use?
Yes, you can set any node as content of a dialog box.
alert.getDialogPane().setContent(grid);
Here is a sell/buy alert for frozen orange contracts formatted as grid content.
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class GridAlert extends Application {
#Override
public void start(Stage stage) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setHeaderText("Frozen Orange Juice Contracts");
GridPane grid = new GridPane();
grid.addRow(0, new Label("Sell"), new Label("142"));
grid.addRow(1, new Label("Buy"), new Label("29"));
grid.setHgap(30);
ColumnConstraints right = new ColumnConstraints();
right.setHalignment(HPos.RIGHT);
grid.getColumnConstraints().setAll(new ColumnConstraints(), right);
alert.getDialogPane().setContent(grid);
Button showAlert = new Button("Show Alert");
showAlert.setOnAction(event -> alert.showAndWait());
HBox layout = new HBox(10);
layout.getChildren().addAll(
showAlert
);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
alert.initOwner(stage);
alert.initModality(Modality.WINDOW_MODAL);
}
public static void main(String[] args) {
launch();
}
}

Insert small icon (Emotion) in text message like Facebook chat message [duplicate]

I am trying to add an emoji to my chat program when my client types :)
I am trying to add this in the FXML controller. I have captured when the user types :) using the following code snippet :
if(chat.contains(":)")) {
...
}
My chat is printed into a textarea named taChat
taChat.appendText(chat + '\n');
Any help is appreciated!
A better approach would be to use TextFlow instead of using TextArea.
Advantages :
Individual Text are treated as children to the TextFlow. They can be added and accessed individually.
ImageView can be added directly to the TextFlow as a child.
A simple chat window with support for smiley :)
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
public class ChatWindowWithSmiley extends Application {
public void start(Stage primaryStage) {
TextFlow textFlow = new TextFlow();
textFlow.setPadding(new Insets(10));
textFlow.setLineSpacing(10);
TextField textField = new TextField();
Button button = new Button("Send");
button.setPrefWidth(70);
VBox container = new VBox();
container.getChildren().addAll(textFlow, new HBox(textField, button));
VBox.setVgrow(textFlow, Priority.ALWAYS);
// Textfield re-sizes according to VBox
textField.prefWidthProperty().bind(container.widthProperty().subtract(button.prefWidthProperty()));
// On Enter press
textField.setOnKeyPressed(e -> {
if(e.getCode() == KeyCode.ENTER) {
button.fire();
}
});
button.setOnAction(e -> {
Text text;
if(textFlow.getChildren().size()==0){
text = new Text(textField.getText());
} else {
// Add new line if not the first child
text = new Text("\n" + textField.getText());
}
if(textField.getText().contains(":)")) {
ImageView imageView = new ImageView("http://files.softicons.com/download/web-icons/network-and-security-icons-by-artistsvalley/png/16x16/Regular/Friend%20Smiley.png");
// Remove :) from text
text.setText(text.getText().replace(":)"," "));
textFlow.getChildren().addAll(text, imageView);
} else {
textFlow.getChildren().add(text);
}
textField.clear();
textField.requestFocus();
});
Scene scene = new Scene(container, 300, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Output
For unicode Emoji support, please visit How to support Emojis

JavaFX button.arm() not working. Are there other ways to?

I am trying to auto press a button in my application so that it triggers a function.
As per the java docs, the .arm() button should do this. But it does not.
I also tried .fire(), but with no luck.
Any ideas on how to get a button be clicked on its own?
button.fire() works and is the right method to use to trigger an automated button press.
Here is a short sample which demonstrates it's use:
A timeline is started which automatically fires a button every second. When the button is fired it increments the counter label below the button.
import javafx.animation.*;
import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Clicker extends Application {
#Override
public void start(Stage stage) throws Exception {
Label counter = new Label("0");
Button clicker = new Button("Auto-clicked");
clicker.setOnAction(event ->
counter.setText(
(1 + Integer.parseInt(counter.getText())) + ""
)
);
Timeline ticker = new Timeline(
new KeyFrame(
Duration.seconds(1),
event -> clicker.fire()
)
);
ticker.setCycleCount(1_000_000);
ticker.play();
VBox layout = new VBox(10, clicker, counter);
layout.setAlignment(Pos.CENTER);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) {
launch(Clicker.class);
}
}

Resources