如果将文本字段设置为只读模式,则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+Cursor键选择文本,但不显示插入符号。有人知道解决办法吗?
发布于 2015-01-12 14:34:14
由尼尔的回答触发,我尝试对我的建议进行快速测试,扩展TextAreaSkin并将caretVisible属性替换为不检查可编辑性的属性。看起来很有效(虽然没有经过彻底的测试)--但是需要对超级程序的私有闪烁属性进行反射访问。很明显在安全受限的情况下是不可能的.
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);
}
};发布于 2015-01-12 12:05:56
我想要同样的东西--一个只读字段,但插入符号可用于导航。我试过:
.text-input:readonly { -fx-display-caret: true; }但没有结果。深入到FX源代码( 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()。我可以在虚拟插入符号上画一个工作--这很难看,但我不确定还有别的方法--看起来你要么可以伪造插入符号,要么可以伪造只读方面(拒绝对控件的所有编辑)。
https://stackoverflow.com/questions/27291536
复制相似问题