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

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

问题描述

我想在JavaFX8中隐藏TextField的输入插入符。我已经尝试将文本填充设置为白色(我的TextField有一个白色的背景),但是我的用户输入也消失了。
$ b $问题是文本字段仍然需要焦点,所以我也想到了一些EventHandler监听用户输入,并按下按键时将焦点放在文本字段上,但是那么我会错过键入的第一个键。



任何知道如何做到这一点的人?或者在JavaFX8中是不可能的?

解决方案

更新为Java 8u40
$ b

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

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

   - fx-display-caret:false; 

有关更多详情,请参阅相关问题:


  • 中创建一个功能请求,请求使用公共支持的方法(例如,添加新的CSS属性来控制插入符外观)更多地控制插入符号。

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




    示例应用程序 p>

    演示如何使用自定义的TextFieldCaretControlSkin。
    $ b

      import javafx.geometry。插图; 
    导入javafx.scene.Scene;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.VBox;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;

    公共类CaretColorizer扩展应用程序{

    @Override
    public void start(Stage stage)throws Exception {
    TextField redCaretTextField = new TextField(Red插入符号);
    redCaretTextField.setSkin(
    )TextFieldCaretControlSkin(
    redCaretTextField,
    Color.RED

    );

    TextField noCaretTextField = new TextField(No Caret);
    noCaretTextField.setSkin(
    )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);
    }
    }


    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天全站免登陆