JavaFX-8使文本插入符号在readonly textarea中可见

时间:2014-12-04 10:23:34

标签: javafx javafx-8

如果将JavaFX文本字段设置为只读模式,则它们不会显示文本插入符号。这是一个例子:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

public class TextAreaReadOnly extends Application {

    public TextAreaReadOnly() {
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        TextArea textarea = new TextArea();
        textarea.setText("This is all\nreadonly text\nin here.");
        textarea.setEditable(false);
        Scene scene = new Scene(textarea, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }    

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

虽然仍然可以使用Shift +光标键选择文本,但不会显示插入符号。有没有人知道这方面的解决方法?

2 个答案:

答案 0 :(得分:3)

Neil's answer触发,我尝试快速测试我的建议以扩展TextAreaSkin并将caretVisible属性替换为不检查可编辑性的属性。似乎工作(虽然没有经过彻底测试) - 但需要反射访问super的私人眨眼属性。显然是脏的,在安全限制的情境中是不可能的......

public static class MyTextAreaSkin extends TextAreaSkin {

    public MyTextAreaSkin(TextArea textInput) {
        super(textInput);
        caretVisible = new BooleanBinding() {
            { bind(textInput.focusedProperty(), textInput.anchorProperty(), 
                    textInput.caretPositionProperty(),
                    textInput.disabledProperty(), displayCaret , blinkProperty() );}
            @Override protected boolean computeValue() {
                return !blinkProperty().get() &&  displayCaret.get() && textInput.isFocused() &&
                        (isWindows() || (textInput.getCaretPosition() == textInput.getAnchor())) 
                        && !textInput.isDisabled(); 
            }
        };
        // rebind opacity to replaced caretVisible property
        caretPath.opacityProperty().bind(new DoubleBinding() {
            { bind(caretVisible); }
            @Override protected double computeValue() {
                return caretVisible.get() ? 1.0 : 0.0;
            }
        });

    }

    BooleanProperty blinkAlias;

    BooleanProperty blinkProperty() {
        if (blinkAlias == null) {
            Class<?> clazz = TextInputControlSkin.class;
            try {
                Field field = clazz.getDeclaredField("blink");
                field.setAccessible(true);
                blinkAlias = (BooleanProperty) field.get(this);
            } catch (NoSuchFieldException | SecurityException 
                   | IllegalArgumentException | IllegalAccessException e) {
                // TBD: errorhandling
                e.printStackTrace();
            }

        }
        return blinkAlias;
    }

}

// usage in a custom TextArea
TextArea textarea = new TextArea() {

    @Override
    protected Skin<?> createDefaultSkin() {
        return new MyTextAreaSkin(this);
    }

};

答案 1 :(得分:2)

我想要同样的东西 - 一个只读字段,但导航可见的插入符号。我试过了:

.text-input:readonly { -fx-display-caret: true; }

但无济于事。深入FX source code (for 2.2),我发现了这个:

caretVisible = new BooleanBinding() {
        { bind(textInput.focusedProperty(), textInput.anchorProperty(), textInput.caretPositionProperty(),
                textInput.disabledProperty(), textInput.editableProperty(), displayCaret, blink);}
        @Override protected boolean computeValue() {
            // RT-10682: On Windows, we show the caret during selection, but on others we hide it
            return !blink.get() && displayCaret.get() && textInput.isFocused() &&
                    (isWindows() || (textInput.getCaretPosition() == textInput.getAnchor())) &&
                    !textInput.isDisabled() &&
                    textInput.isEditable();
        }
    };

看起来似乎没有办法在条件结束时覆盖isEditable()的要求。我可能会在一个虚拟的插入符号上绘画,这很难看,但我不确定是否有另一种方法 - 看起来你可以伪造插入符号或伪造只读方面(拒绝对控件进行所有编辑) )。

相关问题