Java FX Waiting for user login

I am writing an application in JavaFX and would like to create a function that waits for the user to enter text into my TextField and hit Enter before returning (continued).

 private void setupEventHandlers() { inputWindow.setOnKeyPressed(new EventHandler<KeyEvent>() { @Override public void handle(KeyEvent e) { if (e.getCode().equals(KeyCode.ENTER)) { inputWindow.clear(); } } }); } 

Here I clear the text in my TextField when the user presses the Enter key.

Any ideas?

Edit: I will clarify exactly what I'm looking for:

 private void getInput() { do { waitForEventToFire(); } while (!eventFired); } 

Obviously this is just pseudo code, but this is what I am looking for.

+4
source share
2 answers

Solution example

Perhaps you need to display a prompt dialog and use showAndWait to wait for a response from the prompt dialog before continuing, Like JavaFX2: can I pause a background job / service?

Most likely, your situation is a bit simpler than the background task service, and (if you don't have a long task), you can just do everything in the JavaFX application thread. I created a simple sample solution that just runs everything in the JavaFX application stream.

The following is an example program:

prompteddataprompt

Each time some missing data is encountered, an invitation dialog box is displayed and is waiting for the user to fill in the missing data (user responses highlighted in green in the screenshot above).

 import javafx.application.Application; import static javafx.application.Application.launch; import javafx.event.*; import javafx.geometry.Pos; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; public class MissingDataDemo extends Application { private static final String[] SAMPLE_TEXT = "Lorem ipsum MISSING dolor sit amet MISSING consectetur adipisicing elit sed do eiusmod tempor incididunt MISSING ut labore et dolore magna aliqua" .split(" "); @Override public void start(Stage primaryStage) { VBox textContainer = new VBox(10); textContainer.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;"); primaryStage.setScene(new Scene(textContainer, 300, 600)); primaryStage.show(); TextLoader textLoader = new TextLoader(SAMPLE_TEXT, textContainer); textLoader.loadText(); } public static void main(String[] args) { launch(args); } } class TextLoader { private final String[] lines; private final Pane container; TextLoader(final String[] lines, final Pane container) { this.lines = lines; this.container = container; } public void loadText() { for (String nextText: lines) { final Label nextLabel = new Label(); if ("MISSING".equals(nextText)) { nextLabel.setStyle("-fx-background-color: palegreen;"); MissingTextPrompt prompt = new MissingTextPrompt( container.getScene().getWindow() ); nextText = prompt.getResult(); } nextLabel.setText(nextText); container.getChildren().add(nextLabel); } } class MissingTextPrompt { private final String result; MissingTextPrompt(Window owner) { final Stage dialog = new Stage(); dialog.setTitle("Enter Missing Text"); dialog.initOwner(owner); dialog.initStyle(StageStyle.UTILITY); dialog.initModality(Modality.WINDOW_MODAL); dialog.setX(owner.getX() + owner.getWidth()); dialog.setY(owner.getY()); final TextField textField = new TextField(); final Button submitButton = new Button("Submit"); submitButton.setDefaultButton(true); submitButton.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent t) { dialog.close(); } }); textField.setMinHeight(TextField.USE_PREF_SIZE); final VBox layout = new VBox(10); layout.setAlignment(Pos.CENTER_RIGHT); layout.setStyle("-fx-background-color: azure; -fx-padding: 10;"); layout.getChildren().setAll( textField, submitButton ); dialog.setScene(new Scene(layout)); dialog.showAndWait(); result = textField.getText(); } private String getResult() { return result; } } } 

Current tooltip dialog library

The ControlsFX library has a pre-written prompt dialog that will process the prompt dialog.

Event Handler Handler Handling and Waiting

Would you like to:

a function that expects the user to enter text in their text filter and hit enter.

By definition, this is an EventHandler . EventHandler "is called when a certain event of the type for which this handler is registered" occurs.

When an event occurs, your event handler will fire, and you can do whatever you want in the event handler β€” you don’t need and should never have a closed wait loop for the event.

Creating a TextField Action Event Handler

Instead of placing an event handler in a window, as you have in your question, it is probably better to use a special event handler in the text field using textField.setOnAction :

 textField.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { // enter has been pressed in the text field. // take whatever action has been required here. } ); 

If you place a text field in the dialog box with the default button set for the dialog, you do not need to install an event handler for the text field, since the default button of the dialog box will correctly select and process the input event.

+6
source

I would like to use ControlsFx, but I was not able to switch to the Java 1.8 runtime, so I had to create a dialog component from scratch. Here is what I came up with:

 private static Response buttonSelected = Response.CANCEL; /** * Creates a traditional modal dialog box * * @param owner the calling Stage that is initiating the dialog. * @param windowTitle text that will be displayed in the titlebar * @param greeting text next to icon that provides generally what to do (ie "Please enter the data below") * @param labelText label text for the input box (ie "Number of widgets:") * @return If user clicks OK, the text entered by the user; otherwise if cancel, NULL. */ public static String prompt(final Stage owner, final String windowTitle, final String greeting, final String labelText) { //overall layout pane BorderPane root = new BorderPane(); root.setPadding(PADDING); Scene scene = new Scene(root); final Dialog dial = new Dialog(windowTitle, owner, scene, MessageType.CONFIRM); final Button okButton = new Button("OK"); okButton.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { dial.close(); buttonSelected = Response.YES; } }); Button cancelButton = new Button("Cancel"); cancelButton.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { dial.close(); buttonSelected = Response.NO; } }); HBox headerGreeting = new HBox(); headerGreeting.setSpacing(SPACING_SMALL); Text messageText = new Text(greeting); messageText.setFont(new Font(messageText.getFont().getName(), 14)); headerGreeting.getChildren().addAll(icon, messageText); root.setTop(headerGreeting); //setup input controls HBox textHBox = new HBox(10); TextField input = new TextField(); Label label = new Label(); label.setText(labelText); label.setLabelFor(input); textHBox.getChildren().addAll(label, input); //create buttons HBox buttons = new HBox(); buttons.setAlignment(Pos.CENTER); buttons.setSpacing(SPACING); buttons.getChildren().addAll(okButton, cancelButton); root.setCenter(buttons); //put controls and buttons in a vertical container, add to root component VBox container = new VBox(20); container.setPadding(new Insets(15, 12, 15, 12)); container.getChildren().addAll(textHBox, buttons); root.setCenter(container); //handle enter key root.setOnKeyReleased(new EventHandler<KeyEvent>() { final KeyCombination combo = new KeyCodeCombination(KeyCode.ENTER); public void handle(KeyEvent t) { if (combo.match(t)) { okButton.fire(); } } }); input.requestFocus(); //set focus to the input box. dial.showDialog(); if (buttonSelected.equals(Response.YES)) { return input.getText(); } else { //cancel return null; } } 

My test post looks like this, so you can quickly run the code above:

 import javafx.application.Application; import javafx.stage.Stage; public class FXOptionsPaneTest extends Application { @Override public void start(Stage primaryStage) throws Exception { String response = FXOptionsPane.prompt(primaryStage, "Create a new Study...", "Please enter the below information.", "Study Name:"); System.out.println(response); } public static void main(String[] args) { launch(args); } } 
0
source

Source: https://habr.com/ru/post/1483452/


All Articles