4

我正在用 JavaFX 编写一个应用程序,并想创建一个函数,等待用户将文本输入我的TextFieldEnter在返回(继续)之前点击。

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

TextField在这里,当用户按下 Enter 键时,我会清除其中的文本。

有任何想法吗?

编辑:我会确切地澄清我在寻找什么:

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

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

4

2 回答 2

6

样品溶液

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

您的情况可能比后台任务服务简单一些,并且(除非您涉及长时间运行的任务),您可以在 JavaFX 应用程序线程上执行所有操作。我创建了一个简单的示例解决方案,它只在 JavaFX 应用程序线程上运行所有内容

这是示例程序的输出:

提示数据迅速的

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

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;
    }
  }
}

现有提示对话框库

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

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

你想要:

一个等待用户在我的 TextField 中输入文本并点击“enter”的函数。

根据定义,这就是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.
    }
);

如果您将文本字段放置在对话框中并为对话框设置了默认按钮,则无需为文本字段设置事件处理程序,因为对话框的默认按钮将拾取并适当地处理 enter 键事件。

于 2013-05-29T22:13:45.887 回答
0

我本来想使用 ControlsFx,但我无法升级到 Java 1.8 运行时,所以不得不从头开始构建一个对话框组件。这是我想出的:

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 (i.e. "Please enter the data below")
 * @param labelText   label text for the input box (i.e. "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;
    }
}

我的测试工具看起来像这样,所以你可以相当快地运行上面的代码:

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);
    }
}
于 2014-04-24T02:18:46.750 回答