JavaFX Textfield - 释放up键时覆盖默认行为 [英] JavaFX Textfield - Override default behaviour when up key is released

查看:152
本文介绍了JavaFX Textfield - 释放up键时覆盖默认行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在释放向上键时将TextField的光标/插入符号设置为结尾:

I am trying to set TextField's cursor/caret to be at the end when the up key is released:

private void setTextFieldBehaviour() {
    _textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent key) {
            switch (key.getCode()) {
                case UP:
                    if (key.isControlDown()) {
                          ... // Do something for Ctrl + UP key
                          break;
                    } else {
                      ... // Do something for plain old UP key
                      _textField.end();
                      // _textField's cursor will go back 
                      // to the front when the UP key is released!
                      break;
                    }
                default:
                    break;
            }
        }
    });
}

但是,正如上面代码的评论所述,因为我只是覆盖了方法 setOnKeyPressed ,这意味着释放密钥时的默认行为将为JavaFX的TextField启动,

However, as stated in the above code's comment, because I am only overriding the method setOnKeyPressed, this means that the default behaviour when the keys are released will kick in for JavaFX's TextField,

因此,这使得在释放UP键时很难将TextField光标设置在最后。

Consequently, this makes it difficult to set the TextField cursor at the end when the UP key is released.

我想覆盖 setOnKeyReleased 仅适用于UP(如下所示),但这会产生光标跳到TextField前面的丑陋效果,然后跳到最后。

I thought of overriding setOnKeyReleased just for UP (as shown below), but this has the ugly effect of the cursor jumping to the front of the TextField, then jumping back at the end.

_textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent key) {
            switch(key.getCode()) {
                case UP:
                    if(!(key.isControlDown())) {
                      ... // Do something for plain old UP key
                      _textField.end();
                      // _textField's cursor will go back 
                      // to the front when the UP key is released
                      // then jump back to the end again!
                      break;
                    }
                default:
                    break;
            }

        }
    });

或者直观地看出它是这样的:

Or visually, this is how it appears:

在按下任何内容之前://光标放在字符串的末尾

TextField [oldString |]

按UP: //光标位于字符串前面

TextField [| oldString]

释放UP://光标返回字符串

TextField [newString |]

Before pressing anything: // Cursor is placed at the end of the string
TextField[oldString |]
Press UP: // Cursor is in front of string
TextField[| oldString]
Release UP: // Cursor is back of string
TextField[newString |]

所以目前,我可以将它设置为后面,但这需要光标跳转。反正有没有做得更好?

So currently, I am able to set it to go to the back, but this entails the cursor jumping around. Is there anyway to do it better?

推荐答案

看看那些展示2种实现方法的示例应用程序:

look at those sample application that show 2 way to achieve this:

设置事件处理程序并使用事件。

by setting the event Handler and consuming the event.

public class main extends Application {

@Override
public void start(Stage primaryStage) {

    HBox root = new HBox();

    TextField textField = new TextField();

    textField.setOnKeyPressed(e -> {

        if (e.getCode().equals(KeyCode.UP)) {
            textField.end();
            e.consume(); // this stop propagating the event
        }

    }); 

    root.getChildren().add(textField);
    Scene scene = new Scene(root, 300, 300);
    primaryStage.setScene(scene);
    primaryStage.show();
}

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

或过滤事件并停止传播

public class main extends Application {

@Override
public void start(Stage primaryStage) {

    HBox root = new HBox();

    TextField textField = new TextField();

    textField.addEventFilter(KeyEvent.KEY_PRESSED, e -> { //event filter first catch the event

        if(e.getCode().equals(KeyCode.UP)){
            textField.end();
            e.consume(); // this stop propagating the event 
        }
    });


    root.getChildren().add(textField);
    Scene scene = new Scene(root, 300, 300);
    primaryStage.setScene(scene);
    primaryStage.show();
}

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

你应该看看 http://docs.oracle.com/javase/8/javafx/events-tutorial/events.htm 了解如何在javaFX中处理事件。

You should look at http://docs.oracle.com/javase/8/javafx/events-tutorial/events.htm to learn how event are processed in javaFX.

这篇关于JavaFX Textfield - 释放up键时覆盖默认行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆