Why are style classes stored in a List instead of a Set?

Tomas Mikula tomas.mikula at gmail.com
Thu May 21 05:39:50 UTC 2015


Hi Roland,

I don't know why it is a List (maybe it is just copying it being a
list in the DOM), but it makes sense to me that a style class can be
present multiple times (for that matter, a multi-set would work just
as well):

If I have two places in my code that add and remove the same style
class, it makes sense to me the style class being removed after both
of them have removed it, not just the first one. Say that I have two
different conditions, either of which makes a node "highlighted". When
both are true, and then one of them stops being true, I still want the
node to stay highlighted.

Why do you need to make a contains-check?

Tomas

On Thu, May 21, 2015 at 1:17 AM, Roland C <roland.cidar at gmail.com> 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!


More information about the openjfx-dev mailing list