【问题标题】:Do you know method to get the JavaFX Text node of a TextArea without using the CSS properties?你知道在不使用 CSS 属性的情况下获取 TextArea 的 JavaFX Text 节点的方法吗?
【发布时间】:2018-09-08 14:38:57
【问题描述】:

我希望找到一种方法来获取 TextArea 的 Text 节点而不使用 CSS 属性。 JavaFX 的 Text 节点公开了 setUnderline(boolean) 方法 通过它我可以设置 TextArea 的 underline 属性;相反,TextArea 不公开相同的方法。此外,TextArea.getText() 方法返回一个 String 而不是 Text 对象。 所以,我解决了以下问题: 在代码中,

// Fields..
private final PseudoClass pseudoClass = PseudoClass.getPseudoClass("underlined");
private final SimpleBooleanProperty underlinedProperty = new SimpleBooleanProperty(false);
private final TextArea textArea = new TextArea();

[...]

// In a method (ex. in the constructor)..
{
    textArea.setId("textArea"); 
    underlinedProperty.addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
            Node node = textArea.getScene().lookup("#textArea .text");  
            node.pseudoClassStateChanged(pseudoClass, newValue);
        }
    });
}

[...]

// The class exposes the getter method for the underlinedProperty
public SimpleBooleanProperty getUnderlinedProperty() {
    return underlinedProperty;
}

现在,我使用以下代码创建了一个 CSS 表:

#textArea .text {
    /* some styles */
    -fx-underline: false;
}

#textArea .text:underlined {
    -fx-underline: true;
}

最后,上面的类在其他一些类中被调用:

{
    'handleOfClassInPoint1'.getUnderlineProperty().set(true); // or false
}

问题在于lookup() 方法:仅当所有fx 节点都已创建时,此方法才返回非空值,即仅在某些图形结果之后。 我希望找到一个不使用 CSS 来设置 TextArea 的下划线属性的过程(例如,切换按钮管理下划线属性:如果选择了切换,则 TextArea 的文本带有下划线)。 任何人都可以帮助我吗? 非常感谢!

【问题讨论】:

  • 为什么不将样式应用到TextArea?您不需要 Text 节点。只需将PseudoClassTextArea 一起使用,然后继续使用您的CSS。然后根据ToggleButton 的选定状态切换PseudoClass。可能将所有行为封装在您自己的 TextArea 类中并公开您的 underlinedProperty 属性。
  • 感谢您的回答。我不确定我是否完全理解了您的解决方案,但我想我已经按照您的建议实现了代码。 CSS id 在 TextArea 类上设置,伪类更改注册符合 ToggleButton 选择。我的代码已经可以工作但有问题。也许我会花几分钟来解释我想做什么:
  • 1.我创建了由 TextArea 和一些按钮组成的便利贴类。这些按钮之一是操作文本区域的字体所必需的。 2. 当用户单击按钮时,将创建一个新舞台并显示到视频中:该舞台包含一个带有按钮和组合框的场景,用于操作字体粗细、姿势、下划线、系列、大小等。通过使用一个简单的窗格,我已经包含了带有操纵字体的短语“Hello world”的预览。
  • 3.当用户单击下划线切换按钮时,上述过程开始,并且预览正确显示在舞台上。 4. 如果我确认所有选定的选项,则关闭阶段,选项将应用于便利贴文本。 5. 现在,用户再次单击字体选项按钮:我希望舞台已加载所有先前的配置。在这种情况下,唯一的问题是下划线属性,因为基于 CSS 的解决方案只有在舞台已经创建并且所有节点都已经存在的情况下才有效。
  • 相反,当用户单击字体选项按钮时,软件能够理解下划线属性处于活动状态。因此,应选择相对切换按钮,并在预览文本下划线。但是,这段代码 Node node = textArea.getScene().lookup("#textArea .text"); node.pseudoClassStateChanged(pseudoClass, newValue);返回 NullPointerException,因为如果尚未创建阶段,则查找方法无法返回具有指定 id 的节点。我希望将那段代码替换为:

标签: java css javafx underline


【解决方案1】:

根据您在问题 cmets 中的描述,我会推荐以下内容。

创建一个自定义的TextArea,看起来像这样:

public class CustomTextArea extends TextArea {

    private static final PseudoClass UNDERLINED = PseudoClass.getPseudoClass("underlined");

    private final BooleanProperty underlined = new SimpleBooleanProperty(this, "underlined") {
        @Override
        protected void invalidated() {
            pseudoClassStateChanged(UNDERLINED, get()); // update PseudoClass state to match 
                                                        // the current value of the property
        }
    };

    // property access methods

    public final void setUnderlined(boolean underlined) {
        this.underlined.set(underlined);
    }

    public final boolean isUnderlined() {
        return underlined.get();
    }

    public final BooleanProperty underlinedProperty() {
        return underlined;
    }

    // constructor      

    public CustomTextArea(String text, boolean underlined) {
        super(text);
        setUnderlined(underlined);
        getStyleClass().add("custom-text-area"); // to allow specific CSS styling
    }

}

然后在你的 CSS 中你会这样做:

.custom-text-area .text {
    -fx-underline: false;
}

.custom-text-area:underlined .text {
    -fx-underline: true;
}

CSS 设置在任何 Text 节点上,该节点是 aCustomTextArea 的后代。 第一个 CSS 规则(没有 ":underlined" 的规则)甚至可能不是必需的,因为 Text 节点的默认 -fx-underlinefalse

然后查询文本是否带下划线很简单,只需调用area.isUnderlined(),其中areaCustomTextArea 的一个实例。

要保持正确的视觉状态,您可以将CustomTextAreaunderlined 属性双向绑定到ToggleButtonselected 属性。当一个发生变化时,另一个将反映该变化。

如果您只想设置特定CustomTextArea 的样式,那么您仍然可以给它一个ID,并在CSS 中使用#ID 引用它。

【讨论】:

  • 斯劳,感谢您的支持。您的解决方案有效!
猜你喜欢
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多