JavaFx when I open dialog.showAndWait() how to focus TextArea - javafx

public Node dialog(){
Pane root = new Pane();
root.setPrefWidth(200);
root.setPrefHeight(200);
Button button = new Button("Dialog");
button.setOnAction(event -> {
Dialog dialog = new Dialog();
TextArea textArea = new TextArea();
dialog.getDialogPane().setContent(textArea);
ButtonType ok = new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
ButtonType cancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
DialogPane dialogPane = dialog.getDialogPane();
dialogPane.getButtonTypes().addAll(ok, cancel);
textArea.requestFocus();
dialog.showAndWait();
});
root.getChildren().add(button);
return root;
}
I try use textArea.requestFocus(); before dialog.showAndWait(); but when dialog open it always
focus OK Button. How to foucs textArea when dialog first open?

This should do the trick:
dialog.setOnShown(event -> {
Platform.runLater(textArea::requestFocus);
event.consume();
});
dialog.showAndWait();

Related

How do I set a tab for tabpane in Javafx?

I am trying to create a button in my tabpane, that can switch to another tab(0 → 1).
This is for creating a chat program using Javafx, and I'm currently using Scenebuilder to design the GUI.
myTabPane_1.getSelectionModel().select(1);
I expect it to change from tab index # '0' to index # '1', but it stays the same.
There is something that you are not showing in your code and because you did not supply enough information no one will be able to help you modify your code so here is a working example of how to change tabs using index and by passing the actual tab
public class Main extends Application {
#Override
public void start(Stage stage) {
VBox vBox = new VBox();
vBox.setAlignment(Pos.CENTER);
Tab tabOne = new Tab("Tab One", new Label("This is Tab One"));
Tab tabTwo = new Tab("Tab Two", new Label("This is Tab Two"));
TabPane tabPane = new TabPane();
tabPane.getTabs().addAll(tabOne, tabTwo);
vBox.getChildren().add(tabPane);
Button button = new Button("Change Tab by passing index");
button.setOnAction(event -> {
if(tabPane.getSelectionModel().getSelectedItem().getText().equals("Tab One"))
tabPane.getSelectionModel().select(1);
else
tabPane.getSelectionModel().select(0);
});
vBox.getChildren().add(button);
Button buttonTwo = new Button("Change Tab by passing tab");
buttonTwo.setOnAction(event -> {
if(tabPane.getSelectionModel().getSelectedItem().getText().equals("Tab One"))
tabPane.getSelectionModel().select(tabTwo);
else
tabPane.getSelectionModel().select(tabOne);
});
vBox.getChildren().add(buttonTwo);
stage.setScene(new Scene(vBox));
stage.show();
}
}

JavaFX custom dialog set Layout of node

We created a Custom Dialog without an FXML file. We are using JavaFX 8.
The dialog loads and functions as expected but we can not move the Buttons and the TextField to enhance the styling.
We have tried to use tf.setLayoutY(50) this has no effect.
We used this tf.setPromptText("This Works ?") and it works.
We would rather not use css to accomplish this styling.
And we will consider a FXML file if we can keep the two event handlers that force data to be entered in the TextField.
So the question is: How to style this Custom Dialog?
The code is a mess as it includes some concepts we tried:
public void CustomDialog() {
Dialog dialog = new Dialog<>();
dialog.setResizable(false);
final Window window = dialog.getDialogPane().getScene().getWindow();
stage = (Stage) window;
stage.setMinHeight(600);
stage.setMinWidth(400);
TextField tf = new TextField();
tf.setLayoutX(10);
tf.setLayoutY(50);
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
dialog.getDialogPane().getChildren().add(tf);
dialog.getDialogPane().setContent(tf);
// Create an event filter that consumes the action if the text is empty
EventHandler<ActionEvent> filter = event -> {
if (tf.getText().isEmpty()) {
event.consume();
}
};
// lookup the buttons
ButtonBase okButton = (Button) dialog.getDialogPane().lookupButton(ButtonType.OK);
Button cancelButton = (Button) dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
// add the event-filter
okButton.addEventFilter(ActionEvent.ACTION, filter);
cancelButton.addEventFilter(ActionEvent.ACTION, filter);
stage.setOnCloseRequest(event -> {
if (tf.getText().isEmpty()) {
event.consume();
}
}
//Scene scene = new Scene(root);
//dialogStage.setScene(scene);
dialog.initModality(Modality.APPLICATION_MODAL);
//dialogStage.setAlwaysOnTop(true);
//dialogStage.setResizable(false);
tf.setPromptText("This Works ?");
tf.requestFocus();// This does not work
dialog.showAndWait();
}
Grendel we enhanced your answer so anyone who comes by and sees the code you posted in your question will understand as you said it was a mess
Your posted answer was real old school but less work perhaps than building a FXML file
Besides it is good to know some old school tricks
public void NewDialog(){
Label lblAmt = new Label("Enter Amount");
Button btnOK = new Button("OK");
TextField txtAmt = new TextField();
AnchorPane secondaryLayout = new AnchorPane();
secondaryLayout.setStyle("-fx-border-color:red;-fx-border-width:10px; -fx-background-color: lightblue;");
secondaryLayout.getChildren().addAll(lblAmt,btnOK,txtAmt);
lblAmt.setLayoutX(30);
lblAmt.setLayoutY(30);
txtAmt.setLayoutX(164);
txtAmt.setLayoutY(25);
txtAmt.setMaxWidth(116);
btnOK.setLayoutX(190);
btnOK.setLayoutY(100);
btnOK.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
lblAmt.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
txtAmt.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
Scene secondScene = new Scene(secondaryLayout, 300, 180);
EventHandler<ActionEvent> filter = event -> {
if(txtAmt.getText().isEmpty()) {
event.consume();
}
};
// New window (Stage)
Stage newWindow = new Stage();
newWindow.initStyle(StageStyle.UNDECORATED);
//newWindow.initModality(Modality.APPLICATION_MODAL);
newWindow.setResizable(false);
newWindow.setTitle("Second Stage");
newWindow.setScene(secondScene);
btnOK.addEventHandler(ActionEvent.ACTION,filter);
btnOK.setOnAction(evt -> {
String str = txtAmt.getText();
System.out.println("################ str "+str);
if(txtAmt.getText().equals("")) {
evt.consume();
txtAmt.requestFocus();
}else{
newWindow.close();
}
});
newWindow.setOnCloseRequest(event -> {
if(txtAmt.getText().isEmpty()) {
event.consume();
}
});
txtAmt.requestFocus();
newWindow.showAndWait();
}

Controlling the ESC key action in a JavaFX dialog

I have a JavaFX application which will initially show a Login dialog for user to key in user name and password. See below source codes.
If the user clicks on the "Connect" button, the application will perform the login with the entered user name and password, hides the Login dialog, and then shows the main window.
If the user clicks on the "Exit" button or the "X" close button, an alert will be shown to get the user's confirmation. If the user confirms, the application exits.
My problem is what happens when the user press the Escape key when the Login dialog is showing. When this key is pressed, the exit confirmation alert will be shown and immediately after that it will be closed. So what we see is the exit confirmation dialog showing up momentarily whenever the Escape key is pressed.
Why is this happening?
I want pressing the Escape key to be equivalent to clicking on the "Exit" or "X" button. That is, when Escape key is pressed, the exit confirmation dialog is shown.
Alternatively, is it possible to disable the Escape key altogether?
Thanks in advance.
public class TestApp extends Application {
private Stage primaryStage;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
this.primaryStage = stage;
HBox pane = new HBox();
pane.setAlignment(Pos.CENTER);
Scene scene = new Scene(pane, 300, 300);
stage.setScene(scene);
showLoginDialog();
}
public void showLoginDialog() {
Dialog<String> loginDialog = new Dialog<>();
loginDialog.setTitle("Login");
loginDialog.setHeaderText("Enter User Name and Password to login.");
loginDialog.setResizable(false);
Label userNameLabel = new Label("User Name:");
Label passwordLabel = new Label("Password:");
TextField userNameField = new TextField();
userNameField.setMaxWidth(Double.MAX_VALUE);
PasswordField passwordField = new PasswordField();
passwordField.setMaxWidth(Double.MAX_VALUE);
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 35, 20, 35));
grid.add(userNameLabel, 1, 1);
grid.add(userNameField, 2, 1);
grid.add(passwordLabel, 1, 2);
grid.add(passwordField, 2, 2);
loginDialog.getDialogPane().setContent(grid);
loginDialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
loginDialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
Button connectButton = (Button) loginDialog.getDialogPane().lookupButton(ButtonType.OK);
connectButton.setText("Connect");
connectButton.addEventFilter(ActionEvent.ACTION, event -> {
// perform login here
loginDialog.hide();
primaryStage.show();
event.consume();
});
Button exitButton = (Button) loginDialog.getDialogPane().lookupButton(ButtonType.CANCEL);
exitButton.setText("Exit");
exitButton.addEventFilter(ActionEvent.ACTION, event -> {
handleExit();
event.consume();
});
Stage stage = (Stage) loginDialog.getDialogPane().getScene().getWindow();
stage.setOnCloseRequest(event -> {
handleExit();
event.consume();
});
stage.show();
}
private void handleExit() {
Alert alert = new Alert(AlertType.CONFIRMATION, "", ButtonType.YES, ButtonType.NO);
alert.setHeaderText("Confirm exit?");
alert.resultProperty().addListener((observable, previous, current) -> {
if (current == ButtonType.YES) {
System.exit(1);
}
});
alert.show();
}
}
See if this makes a difference:
private void handleExit() {
Alert alert = new Alert(AlertType.CONFIRMATION, "", ButtonType.YES, ButtonType.NO);
alert.setHeaderText("Confirm exit?");
Optional<ButtonType> result = alert.showAndWait();
if(result.get() == ButtonType.YES)
{
Platform.exit();
}
alert.show();
}

How to align/move Vaadin buttons

I am learning Vaadin and I would like to move two buttons to the bottom of pop up window with spacing in between the buttons. I'm pretty sure I would have to override the button css in my theme but how do I change the absolute location of the button in java code?
Here is my code: A simple button with a click listener that calls a pop up method (subwindow). In the below code I'm trying to move the yes button to the bottom of pop up window.
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();
Button helloButton = new Button("Click Me");
helloButton.addClickListener(e -> helloPopUp());
layout.setMargin(true);
layout.setSpacing(true);
layout.addComponents(helloButton);
setContent(layout);
}
private void helloPopUp() {
Window subWindow = new Window("Pop Up");
HorizontalLayout subContent = new HorizontalLayout();
AbsoluteLayout layout2 = new AbsoluteLayout();
subContent.setMargin(true);
Label text = new Label( "Hello Pop Up" , ContentMode.PREFORMATTED);
subContent.addComponent(text);
Button yes = new Button("Yes");
Button no = new Button("No");
layout2.addComponent(yes, "left: 50px; bottom: 0px;");
subContent.addComponents(layout2);
subContent.addComponent(no);
subWindow.setContent(subContent);
UI.getCurrent().addWindow(subWindow);
}
Here is a way to do this without using AbsoluteLayout
private void helloPopUp() {
Window subWindow = new Window("Pop Up");
VerticalLayout subContent = new VerticalLayout();
subContent.setMargin(true);
Label text = new Label( "Hello Pop Up" , ContentMode.PREFORMATTED);
subContent.addComponent(text);
Button yes = new Button("Yes");
Button no = new Button("No");
HorizontalLayout buttonsLayout = new HorizontalLayout();
buttonsLayout.addComponents(yes, no);
buttonsLayout.setSpacing(true);
subContent.addComponent(buttonsLayout);
subContent.setComponentAlignment(buttonsLayout, Alignment.BOTTOM_LEFT);
subWindow.setContent(subContent);
UI.getCurrent().addWindow(subWindow);
}

Set image on left side of dialog

I created this very simple example for JavaFX alert dialog for JavaFX8u40.
public class MainApp extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
private Stage stage;
#Override
public void start(Stage primaryStage) throws Exception
{
Button create = new Button("Create Alert");
create.setTooltip(new Tooltip("Create an Alert Dialog"));
create.setOnAction(e ->
{
createAlert();
});
primaryStage.setScene(new Scene(create));
primaryStage.show();
stage = primaryStage;
}
protected Alert createAlert()
{
Alert alert = new Alert(AlertType.WARNING);
Image image1 = new Image("http://www.mcaprojecttraining.com/images/java-big-icon.png");
ImageView imageView = new ImageView(image1);
alert.setGraphic(imageView);
alert.initModality(Modality.APPLICATION_MODAL);
alert.initOwner(stage);
alert.getDialogPane().setContentText("Some text");
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent(response -> System.out.println("The alert was approved"));
return alert;
}
}
I'm interested how I can set the image on the left side of the dialog.
Did someone manage to change the side of the image?
If you have a look at how the header is constructed, you'll find a GridPane node to layout a Label on the left and a StackPane for the icon.
If you want to reverse the cells order by code, you can do it, but it will be overriden every time updateHeaderArea() is called.
My suggestion is using this public API:
dialogPane.setHeader(Node header);
dialogPane.setGraphic(Node graphic);
providing a header with an icon on the left and a label, and a null graphic.
Using the same approach as DialogPane, we could add another GridPane as header:
protected Alert createAlert(){
Alert alert = new Alert(AlertType.WARNING);
alert.initModality(Modality.APPLICATION_MODAL);
alert.initOwner(stage);
alert.getDialogPane().setContentText("Some text");
DialogPane dialogPane = alert.getDialogPane();
GridPane grid = new GridPane();
ColumnConstraints graphicColumn = new ColumnConstraints();
graphicColumn.setFillWidth(false);
graphicColumn.setHgrow(Priority.NEVER);
ColumnConstraints textColumn = new ColumnConstraints();
textColumn.setFillWidth(true);
textColumn.setHgrow(Priority.ALWAYS);
grid.getColumnConstraints().setAll(graphicColumn, textColumn);
grid.setPadding(new Insets(5));
Image image1 = new Image("http://www.mcaprojecttraining.com/images/java-big-icon.png");
ImageView imageView = new ImageView(image1);
imageView.setFitWidth(64);
imageView.setFitHeight(64);
StackPane stackPane = new StackPane(imageView);
stackPane.setAlignment(Pos.CENTER);
grid.add(stackPane, 0, 0);
Label headerLabel = new Label("Warning");
headerLabel.setWrapText(true);
headerLabel.setAlignment(Pos.CENTER_RIGHT);
headerLabel.setMaxWidth(Double.MAX_VALUE);
headerLabel.setMaxHeight(Double.MAX_VALUE);
grid.add(headerLabel, 1, 0);
dialogPane.setHeader(grid);
dialogPane.setGraphic(null);
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent(response -> System.out.println("The alert was approved"));
return alert;
}
And this is what you will see:

Resources