Set Graphic to label


Question

I want to set Label to graphic. I tested this code:

    private static final ImageView livePerformIcon;

        static
        {
            livePerformIcon = new ImageView(MainApp.class.getResource("/images/Flex.jpg").toExternalForm());
        }

final Label label = new Label();
            label.setStyle("-fx-background-image: url(\"/images/Flex.jpg\");");

            livePerformIcon.setFitHeight(20);
            livePerformIcon.setFitWidth(20);
            label.setGraphic(livePerformIcon);

But I don't see any image.

The only way that I found to make it work is this:

label.setStyle("-fx-background-image: url(\"/images/Flex.jpg\");");

Is there a way to solve this?

1
2
3/24/2014 10:07:41 AM

Not sure, but AFAIK controls should be created on the JavaFX Application thread, but you're creating ImageView in a static initializer, which I'm not sure if it's executed on the Application thread.

Besides: Do you really want livePerformIcon to be static???

1
3/24/2014 11:01:12 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon