首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >检查在javafx中选择了哪个文本框

检查在javafx中选择了哪个文本框
EN

Stack Overflow用户
提问于 2018-06-14 06:21:42
回答 2查看 1.3K关注 0票数 2

我有一个带有多个文本框的javafx应用程序,用户可以在其中输入信息。我还有一个内置在应用程序中的键盘,当按下它时,它会将该文本添加到文本框中。

我的问题是,由于我有多个文本框,我不知道该将按钮文本添加到哪个框中。在javafx中,有没有一种方法可以检查用户是否点击了某个文本框,这样我就可以检查哪个文本框被选中并将文本添加到其中?

EN

回答 2

Stack Overflow用户

发布于 2018-06-14 14:30:22

您可以使用活动场景的Scene.focusOwner属性来获取焦点节点。检查它是否为TextInputControl,并为所单击的按钮调用适当的方法。请注意,如果某个按钮的focusTraversabletrue,则单击该按钮可能会移动焦点。(默认情况下是这样的。)

代码语言:javascript
复制
@Override
public void start(Stage primaryStage) {
    GridPane grid = new GridPane();
    final Scene scene = new Scene(grid);

    for (int i = 0; i < 4; i++) {
        grid.add(new TextField(), 0, i);

        final String buttonValue = Character.toString((char) ('a'+i));
        Button button = new Button(buttonValue);
        button.setFocusTraversable(false); // prevent buttons from stealing focus
        button.setOnAction(evt -> {
            Node fo = scene.getFocusOwner();
            if (fo instanceof TextInputControl) {
                ((TextInputControl) fo).replaceSelection(buttonValue);
            }
        });
        grid.add(button, 1, i);
    }


    primaryStage.setScene(scene);
    primaryStage.show();
}
票数 5
EN

Stack Overflow用户

发布于 2018-06-14 06:40:28

您应该为每个TextFieldfocusProperty创建一个侦听器并设置一个实例变量。

一旦您有了对当前关注的TextField的全局引用,您就可以对其执行您选择的任何处理。

下面是一个快速演示的应用程序。我已经在代码本身中包含了一些额外的细节:

代码语言:javascript
复制
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class Main extends Application {

    // Instance variable to hold the currently-selected TextField
    private TextField selectedTextField;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // Create TextFields
        TextField txt1 = new TextField();
        TextField txt2 = new TextField();
        TextField txt3 = new TextField();
        TextField txt4 = new TextField();

        // This method sets the same change listener on each textfield
        installListener(txt1, txt2, txt3, txt4);

        VBox pane = new VBox(5);
        pane.setPadding(new Insets(5));

        // Add the TextFields to the layout
        pane.getChildren().addAll(
                new HBox(5, new Label("Txt1: "), txt1),
                new HBox(5, new Label("Txt2: "), txt2),
                new HBox(5, new Label("Txt3: "), txt3),
                new HBox(5, new Label("Txt4: "), txt4)
        );

        primaryStage.setScene(new Scene(pane));
        primaryStage.show();
    }

    // Accepts multiple TextFields
    private void installListener(TextField... textFields) {

        // Install the same listener on all of them
        for (TextField textField : textFields) {
            textField.focusedProperty().addListener((observableValue, oldValue, newValue) -> {

                // Set the selectedTextField to null whenever focus is lost. This accounts for the 
                // TextField losing focus to another control that is NOT a TextField
                selectedTextField = null;

                if (newValue) {
                    // The new textfield is focused, so set the global reference
                    selectedTextField = textField;
                    System.out.println("Selected Text: " + selectedTextField.getText());
                }
            });
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50847111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档