The bottom line answer is poor design of the API. There has been discussion about how to handle this, such as having a List implementation that rejects duplicates (but that would break the List contract), or adding new API.

You might be interested to know that, on the CSS implementation side, this List<String> is turned into a Set<StyleClass>.

On 5/21/15 1:17 AM, Roland C wrote:
I was recently toying around with CSS in JavaFX and noticed that I got the
same style multiple times in the style list of my node.

Since the order of the styles is defined by the order in the css file and
not by the order of the list that getStyleClass() of a node returns, I was
wondering if there is a special reason for that.

Example:

application.css

.bg-color-1 {
     -fx-background-color:red; }.bg-color-2 {
     -fx-background-color:green;}

Main.java

public class Main extends Application {
     @Override
     public void start(Stage primaryStage) {
         try {
             BorderPane root = new BorderPane();

             root.getStyleClass().add( "bg-color-1");
             root.getStyleClass().add( "bg-color-2");

             Scene scene = new Scene(root,400,400);
             
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
             primaryStage.setScene(scene);
             primaryStage.show();
         } catch(Exception e) {
             e.printStackTrace();
         }
     }

     public static void main(String[] args) {
         launch(args);
     }}

It doesn't matter if you write

     root.getStyleClass().add( "bg-color-1");
     root.getStyleClass().add( "bg-color-2");

or change the order to

     root.getStyleClass().add( "bg-color-2");
     root.getStyleClass().add( "bg-color-1");

The used style will always be the last in the css file, i. e. "bg-color-2".

*Question*

Why is a List used instead of a Set? A list is less performing than a Set
and it clutters the code if you always have to make a contains-check first.


Thank you very much!

Reply via email to