首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX-8使文本插入符号在只读文本区域中可见。

JavaFX-8使文本插入符号在只读文本区域中可见。
EN

Stack Overflow用户
提问于 2014-12-04 10:23:34
回答 2查看 2.1K关注 0票数 3

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

代码语言:javascript
复制
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+Cursor键选择文本,但不显示插入符号。有人知道解决办法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-12 14:34:14

尼尔的回答触发,我尝试对我的建议进行快速测试,扩展TextAreaSkin并将caretVisible属性替换为不检查可编辑性的属性。看起来很有效(虽然没有经过彻底的测试)--但是需要对超级程序的私有闪烁属性进行反射访问。很明显在安全受限的情况下是不可能的.

代码语言:javascript
复制
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);
    }

};
票数 3
EN

Stack Overflow用户

发布于 2015-01-12 12:05:56

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

代码语言:javascript
复制
.text-input:readonly { -fx-display-caret: true; }

但没有结果。深入到FX源代码( 2.2),我发现了这个:

代码语言:javascript
复制
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()。我可以在虚拟插入符号上画一个工作--这很难看,但我不确定还有别的方法--看起来你要么可以伪造插入符号,要么可以伪造只读方面(拒绝对控件的所有编辑)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27291536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档