java - 属性 "style"不存在或为只读

标签 java javafx

我在使用 JavaFX8 中的选择框时遇到问题。一旦我使用上面的代码,它就可以正常获取下拉列表。

<ChoiceBox fx:id="messageChoiceBox" maxWidth="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="24.0" xmlns:fx="http://javafx.com/fxml">
    <items>
        <FXCollections fx:factory="observableArrayList">
            <String fx:value="Inbox" style="-fx-background-image: url('file:resources/images/message/draft.png');" />
            <String fx:value="Facebook" />
            <String fx:value="Orkut" />
            <String fx:value="LinkedIn" />
            <String fx:value="Google Plus" />
        </FXCollections>
    </items>
    <HBox.margin>
        <Insets right="40.0" top="3.0" />
    </HBox.margin>
</ChoiceBox>

但我的问题是,我还需要为每个 fx:value 使用图像

<String fx:value="Facebook" style="-fx-background-image: url('myPath/facebook.png');" />
<String fx:value="Orkut" style="-fx-background-image: url('myPath/Orkut.png');" />
<String fx:value="LinkedIn" style="-fx-background-image: url('myPath/LinkedIn.png');" />
<String fx:value="Google Plus" style="-fx-background-image: url('myPath/Google Plus.png');" />

一旦我运行这个错误..

Caused by: com.sun.javafx.fxml.PropertyNotFoundException: Property "style" does not exist or is read-only. 
    at javafx.fxml.FXMLLoader$Element.processValue(Unknown Source) 
    at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(Unknown Source)...

有谁知道如何解决这个问题吗?让我知道 有没有办法在FXML中的选择框中添加样式。

非常感谢大家..

最佳答案

由于 java.lang.String 不提供样式属性,因此您必须使用自己的包含值和样式的类。

请注意,ChoiceBox 不支持样式项目,因此我建议使用 ComboBox使用自定义单元工厂代替:

样式字符串类

public class StyledString {

    private final String value;
    private final String style;

    // allows creating instances from fxml with given value and style
    public StyledString(@NamedArg("value") String value, @NamedArg("style") String style) {
        this.value = value;
        this.style = style;
    }

    public String getValue() {
        return value;
    }

    public String getStyle() {
        return style;
    }

}

细胞工厂

public class StyledListCellsFactory implements Callback<ListView<StyledString>, ListCell<StyledString>> {

    @Override
    public ListCell<StyledString> call(ListView<StyledString> param) {
        return new ListCell<StyledString>() {

            @Override
            protected void updateItem(StyledString item, boolean empty) {
                super.updateItem(item, empty);

                if (item == null || empty) {
                    setText(null);
                    setStyle(null);
                } else {
                    setText(item.getValue());
                    setStyle(item.getStyle());
                }
            }

        };
    }

}

fxml

确保导入相关类(StyledStringStyledListCellsFactory)

<ComboBox fx:id="messageChoiceBox" maxWidth="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="24.0" xmlns:fx="http://javafx.com/fxml">
    <cellFactory>
        <StyledListCellsFactory/>
    </cellFactory>
    <items>
        <FXCollections fx:factory="observableArrayList">
            <StyledString value="Inbox" style="-fx-background-image: url('file:resources/images/ib.png');" />
            <StyledString value="Facebook" style="-fx-background-image: url('file:resources/images/fb.png');" />
            <StyledString value="Orkut" style="-fx-background-image: url('file:resources/images/index.jpg');" />
            <StyledString value="LinkedIn" />
            <StyledString value="Google Plus" />
        </FXCollections>
    </items>
    <HBox.margin>
        <Insets right="40.0" top="3.0" />
    </HBox.margin>
</ComboBox>

但是,如果您始终使用图像,请考虑使用图像 url 而不是样式。您仍然可以使用 url 构造样式字符串,也可以使用单元工厂将图像显示为 graphic (有关 graphic 属性的使用示例,请参阅 ComboBox javadoc(矩形可以替换为 ImageView 并将 contentDisplay 属性设置为否则))

关于java - 属性 "style"不存在或为只读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34202880/

相关文章:

java - 将参数传递给 AIDL

java - 如果我在 for 循环之后添加球,为什么球不会出现在框架中?

java - 在 Eclipse 中安装 JML

java - javafx中如何聚焦旅行?

c# - 如何创建起始索引为 1(而不是 0)的 ArrayList

java - 无法解析方法 findViewById ("int")

java - 使用 java sdk 12 的 JFXTextField 的 IllegalAccessException

线程 "main"java.lang.NoClassDefFoundError : javafx/application/Application 中的 JavaFX 异常

java - "Optional<ButtonType>.get()"和 "alert.getResult()"之间的区别

java - 在表格 View 单元格上显示弹出窗口双击 JavaFX