【问题标题】:How do I automatically trigger enter key in Javafx如何在 Javafx 中自动触发回车键
【发布时间】:2016-05-16 08:15:56
【问题描述】:

现在我正在研究 raspberry pi,我在 java、javafx 平台上编写了一些程序。我只想告诉你,我只是 javafx 的初学者。

据此,我只是想在更改文本字段后触发 ENTER 键。我的程序的工作原理是这样的;

1)我创建了一个 masterform fxml,它使用一个文本字段来引导所有其他页面。

2) 我创建了 main 方法,让我可以使用键盘输入一些特定的字符串值,以将它们分配给文本字段以进行页面更改。

3)我有一个桥接 java 页面,它包含在项目中随处使用的全局变量。所以首先我将键盘的值设置为这些全局变量。这些全局变量被创建为字符串属性,用于为任何更改添加 actionlistener。

4)然后我将这些全局变量设置为文本字段。

5)文本字段表示来自键盘的相关值。但不幸的是,如果不按 Enter 键,我无法转发页面。在这种情况下,我想触发此文本字段。但不幸的是,我不知道如何在不按 Enter 的情况下触发 texfield key.因此我决定自动触发为这个文本字段输入键。

我只是简单地使用了机器人方法;

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);

但是没有用。因为我第一次将全局变量设置为textfield后。它没有定义textfield的值被改变了。它是在按下回车键后确定的。

那么在获取全局变量的值后如何触发此文本字段。我想传递如何设置页面,我将向您展示我的程序是如何工作的。

我的代码示例是;

主要方法

 public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);

      for (String strBarcode = scanner.nextLine(); !strBarcode.isEmpty(); 
              strBarcode = scanner.nextLine()) {

          if (strBarcode.equals("distribution")){

          Global.G_MOD.set("distribution");
          System.out.println(Global.G_MOD.get());

      }
}}

GlobalVariables.java(桥页)

public class Global{
public static StringProperty G_MOD = new SimpleStringProperty("");
}

我的 javafx MasterController 页面

public class masterformController implements Initializable {


@FXML
    public TextField tbxBarcode;

@FXML
    void onchangetbxBarcode(ActionEvent event)  {

if(Global.G_MOD.get().equals("distribution")){

        try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/puttolightfx/fxml/page1.fxml"));
            Parent rootpage1 = (Parent)loader.load();

            pnPages.getChildren().clear();
            pnPages.getChildren().add(rootpage1);       


         } catch (IOException ex) {
            Logger.getLogger(masterformController.class.getName()).log(Level.SEVERE, null, ex);
        }
}

@Override
    public void initialize(URL url, ResourceBundle rb) {

Global.G_MOD.addListener(new ChangeListener(){
        @Override
            public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                String Newvalue = (String)newValue; 
                tbxBarcode.setText(Global.G_MOD.get());}
        }); 

}


}

所以一切正常,只是当全局值:Global.G_MOD在texfield上指示时我必须触发文本字段。然后它将根据Global的全局值传递到另一个页面.G_MOD : "分发".

解决方案(已解决):

我在文本字段的侦听器上使用线程解决了我的问题。我放弃了自动触发输入键并专注于文本字段的更改。

我只是决定使用线程来更改文本字段侦听器中的 .fxml 页面。

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        //if you change the UI, do it here !
    }
});

编辑代码:

tbxBarcode.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
            String Newvalue=(String)newValue;
            System.out.println(tbxBarcode.getText());
            Platform.runLater(new Runnable() {
    @Override
    public void run() {
                           if(Global.G_MOD.get().equals("distribution")){

        try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/puttolightfx/fxml/page1.fxml"));
            Parent rootpage1 = (Parent)loader.load();

            pnPages.getChildren().clear();
            pnPages.getChildren().add(rootpage1);       


         } catch (IOException ex) {
            Logger.getLogger(masterformController.class.getName()).log(Level.SEVERE, null, ex);
        }
}
//                }


    }
});
        });

【问题讨论】:

    标签: javafx triggers key textfield enter


    【解决方案1】:

    尝试使用

    textField.fireEvent(new KeyEvent(KeyEvent.KEY_PRESSED, "", "", KeyCode.ENTER, true, true, true, true));
    

    根据文档

    public KeyEvent(EventType<KeyEvent> eventType,
                    String character,
                    String text,
                    KeyCode code,
                    boolean shiftDown,
                    boolean controlDown,
                    boolean altDown,
                    boolean metaDown)
    Constructs new KeyEvent event with null source and target and KeyCode object directly specified.
    Parameters:
    eventType - The type of the event.
    character - The character or sequence of characters associated with the event
    text - A String describing the key code
    code - The integer key code
    shiftDown - true if shift modifier was pressed.
    controlDown - true if control modifier was pressed.
    altDown - true if alt modifier was pressed.
    metaDown - true if meta modifier was pressed.
    Since:
    JavaFX 8.0
    

    您可以参考https://docs.oracle.com/javase/8/javafx/api/javafx/scene/input/KeyEvent.html

    编辑 1 您需要确定必须触发Enter 键事件的时刻。 例如: 如果您的文本字段允许有限数量的字符,那么您可以通过以下方式添加上述代码:

    txtField.textProperty().addListener(new ChangeListener<String>() {
    
                @Override
                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
    
                    if (newValue.length()>30) {
                        txtField.setText(oldValue);
                        txtField.fireEvent(new KeyEvent(KeyEvent.KEY_PRESSED, "", "", KeyCode.ENTER, true, true, true, true));
                    }
                }
            });
    

    这只是一个例子。它可以多次触发您的事件,因此您需要编写代码来触发一次事件。

    【讨论】:

    • “robot.fireEvent”是什么意思。我应该定义什么来代替这个?没有名为“fireEvent”的方法,我应该使用什么来代替这个。
    • fireEvent() 是所有 javafx 控件的内置方法。抱歉提到机器人。我已经编辑了我的答案。将此方法应用于您的 TextField 或您希望自动触发事件的任何其他控件。
    • 我认为它会运行良好。我刚刚尝试过,但在这一行中发生了另一个错误: tbxBarcode.fireEvent(new KeyEtbxBarcodeevent(KeyEvent.KEY_PRESSED, "" , "", KeyCode. ENTER, true, true, true, true)); 和错误:不兼容的类型; int 无法转换为组件 但我不知道哪个值是“int”
    • 只要看一下方法,调用就知道了。您还确定必须使用KeyEtbxBarcodevent 方法而不是KeyEvent 方法吗?我已经按照KeyEvent的方法给出了解决方案。那么你调用的方法的参数可能会有所不同?
    • 我不小心弄到了,正确的是:tbxBarcode.fireEvent(new KeyEvent(KeyEvent.KEY_PRESSED, "" , "", KeyCode.ENTER, true, true, true, true)) ; 但错误是一样的。İnt 无法转换为组件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多