在 JavaFX8 中隐藏 TextField 的输入插入符号 [英] Hide input caret of TextField in JavaFX8

查看:56
本文介绍了在 JavaFX8 中隐藏 TextField 的输入插入符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 JavaFX8 中隐藏 TextField 的输入插入符号.我已经尝试将文本填充设置为白色(我的 TextField 有白色背景),但是我的用户输入也消失了.

问题是文本字段仍然需要焦点,所以我还想到了一些监听用户输入的事件处理程序,并在按下某个键时专注于文本字段,但是我会错过输入的第一个键.

有人知道怎么做吗?或者在 JavaFX8 中是不可能的?

解决方案

Java 8u40 更新

JavaFX 中添加了一个 CSS 样式类来打开和关闭文本字段插入符号,这是(在大多数情况下)比此答案中的自定义皮肤方法更好的解决方案.

此属性可以在 CSS 中应用于任何文本区域或文本字段:

-fx-display-caret: false;

更多详情,请看相关问题:

  • 请求对插入符号进行更多控制使用公开支持的方法(例如添加新的 css 属性来控制插入符号的外观).

    import com.sun.javafx.scene.control.skin.TextFieldSkin;导入 javafx.scene.control.TextField;导入 javafx.scene.paint.Color;公共类 TextFieldCaretControlSkin 扩展 TextFieldSkin {公共 TextFieldCaretControlSkin(TextField textField, Color caretColor) {超级(文本字段);setCaretColor(caretColor);}私有无效 setCaretColor(颜色颜色){caretPath.strokeProperty().unbind();caretPath.fillProperty().unbind();caretPath.setStroke(color);caretPath.setFill(颜色);}}

    示例应用

    演示自定义 TextFieldCaretControlSkin 的使用.

    import javafx.geometry.Insets;导入 javafx.scene.Scene;导入 javafx.scene.control.TextField;导入 javafx.scene.layout.VBox;导入 javafx.scene.paint.Color;导入 javafx.stage.Stage;公共类 CaretColorizer 扩展应用程序 {@覆盖公共无效开始(阶段阶段)抛出异常{TextField redCaretTextField = new TextField("Red Caret");redCaretTextField.setSkin(新的 TextFieldCaretControlSkin(红色CaretTextField,红色));TextField noCaretTextField = new TextField("No Caret");noCaretTextField.setSkin(新的 TextFieldCaretControlSkin(noCaretTextField,颜色.透明));TextField normalTextField = new TextField("Standard Caret");VBox 布局 = 新 VBox(10、红色CaretTextField,noCaretTextField,普通文本字段);layout.setPadding(new Insets(10));stage.setScene(新场景(布局));舞台表演();}公共静态无效主(字符串 [] args){发射(参数);}}

    I would like to hide the input caret of a TextField in JavaFX8. I already tried to set the text fill to white (my TextField has a white background), but then my user input also disappears.

    The problem is that the textfield still needs focus, so I also thought of some EventHandler which listens to user input, and focusses on the textfield when a key is pressed, but then I would miss the first key typed.

    Anyone that knows how to do this? Or is it not possible in JavaFX8?

    解决方案

    Update for Java 8u40

    A CSS style class was added to JavaFX to switch on and off the text field caret which is (in most cases) a better solution than the custom skin approach in this answer.

    This property can be applied in CSS to any text area or text field:

    -fx-display-caret: false;
    

    For more details, see the related question:


    You could use a custom skin to control the text field caret.

    Sample Skin

    Allows you to specify the caret color in the constructor.

    CAUTION: this solution extends from a com.sun class so it is not guaranteed to continue working in future JavaFX versions. (This solution was tested against Java8u25 and worked for that version).

    I suggest that you create a feature request in the JavaFX issue tracker requesting more control over the caret using publicly supported methods (e.g. the addition of new css properties to control the caret appearance).

    import com.sun.javafx.scene.control.skin.TextFieldSkin;
    import javafx.scene.control.TextField;
    import javafx.scene.paint.Color;
    
    public class TextFieldCaretControlSkin extends TextFieldSkin {
        public TextFieldCaretControlSkin(TextField textField, Color caretColor) {
            super(textField);
    
            setCaretColor(caretColor);
        }
    
        private void setCaretColor(Color color) {
            caretPath.strokeProperty().unbind();
            caretPath.fillProperty().unbind();
    
            caretPath.setStroke(color);
            caretPath.setFill(color);
        }
    }
    

    Sample Application

    Demonstrates the use of the custom TextFieldCaretControlSkin.

    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.VBox;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;
    
    public class CaretColorizer extends Application {
    
        @Override 
        public void start(Stage stage) throws Exception {
            TextField redCaretTextField = new TextField("Red Caret");
            redCaretTextField.setSkin(
                    new TextFieldCaretControlSkin(
                            redCaretTextField,
                            Color.RED
                    )
            );
    
            TextField noCaretTextField = new TextField("No Caret");
            noCaretTextField.setSkin(
                    new TextFieldCaretControlSkin(
                            noCaretTextField,
                            Color.TRANSPARENT
                    )
            );
    
            TextField normalTextField = new TextField("Standard Caret");
    
            VBox layout = new VBox(
                    10,
                    redCaretTextField,
                    noCaretTextField,
                    normalTextField
            );
    
            layout.setPadding(new Insets(10));
            stage.setScene(new Scene(layout));
    
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    这篇关于在 JavaFX8 中隐藏 TextField 的输入插入符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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