Java FX等待用户输入

Eth*_*man 4 javafx input javafx-2

我正在JavaFX中编写一个应用程序,并希望创建一个等待用户在返回(继续)之前将文本输入到我的TextField命中的函数Enter.

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

在这里,TextField当用户点击Enter键时,我清除了我的文本.

有任何想法吗?

编辑:我会澄清我正在寻找的东西:

private void getInput() {
    do {
        waitForEventToFire();
    }
    while (!eventFired);
}
Run Code Online (Sandbox Code Playgroud)

显然这只是伪代码,但这正是我正在寻找的.

jew*_*sea 6

样品溶液

也许您想要做的是显示一个提示对话框并使用showAndWait等待来自提示对话框的响应,然后再继续.与JavaFX2类似:我可以暂停后台任务/服务吗?

可能你的情况比后台任务服务简单一点(除非你有一个长时间运行的任务),你可以在JavaFX应用程序线程上做所有事情.我创建了一个简单的示例解决方案,它只运行JavaFX应用程序线程上的所有内容

以下是示例程序的输出:

prompteddata提示

每次遇到一些丢失的数据时,都会显示一个提示对话框并等待用户输入以填写缺失的数据(用户提供的响应在上面的屏幕截图中以绿色突出显示).

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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

现有的提示对话库

ControlsFX库中有一个预先写好的提示对话框,它将为您处理提示对话框显示.

澄清事件处理程序处理和忙等待

你想要:

等待用户在我的TextField中输入文本并点击"回车"的功能.

根据定义,这就是EventHandler.EventHandler"在发生此处理程序的类型的特定事件发生时"调用".

当事件发生时,您的事件处理程序将触发,您可以在事件处理程序中执行任何操作 - 您不需要,并且永远应该为事件执行繁忙的等待循环.

创建TextField操作事件处理程序

不像在问题中那样将事件处理程序放在窗口上,而是使用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.
    }
);
Run Code Online (Sandbox Code Playgroud)

如果将文本字段放在具有为对话框设置的默认按钮的对话框中,则不需要为文本字段设置事件处理程序,因为对话框的默认按钮将拾取并适当地处理输入键事件.