Set Graphic to label

13,082

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???

Share:
13,082
Peter Penzov
Author by

Peter Penzov

Updated on June 05, 2022

Comments

  • Peter Penzov
    Peter Penzov almost 2 years

    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?

  • Peter Penzov
    Peter Penzov about 10 years
    Yes I use it in static Java method.
  • Plutian
    Plutian over 4 years
    Hi and welcome to stackoverflow, and thank you for answering. While this code might answer the question, can you consider adding some explanation for what the problem was you solved, and how you solved it? This will help future readers to understand your answer better and learn from it.