Why are style classes stored in a List instead of a Set?
Roland C
roland.cidar at gmail.com
Thu May 21 07:03:29 UTC 2015
Hello,
the problem is that it isn't clear how the mechanism works. A list suggests
that there is an order. But there isn't. The only order that is valid is
the one of the css file. Took me a while to figure out that it's not a bug
in my code as to why the colors are displayed wrong. It was a "feature" of
the css mechanism.
I noticed it when I created a grid to paint on. Here's an example:
http://stackoverflow.com/questions/30117351/how-to-detect-mouse-movement-over-node-while-button-is-pressed
In that example I paint the cells by adding a "highlight" style class, I
remove the paint by removing the style class. I ended up having 15
"highlight" classes in the list of a node because I painted over it
multiple times. Not that it mattered for the display, it just flood-filled
the style class list unnecessarily.
Roland
On Thu, May 21, 2015 at 8:04 AM, Tom Schindl <tom.schindl at bestsolution.at>
wrote:
> Beside the usecase mentioned by Tomas - the following thread on
> stackoverflow indicate that it might in some cases make a difference!
>
>
> http://stackoverflow.com/questions/15670631/does-the-order-of-classes-listed-on-an-item-affect-the-css
>
> Tom
>
> Von meinem iPhone gesendet
>
> > Am 21.05.2015 um 07:39 schrieb Tomas Mikula <tomas.mikula at gmail.com>:
> >
> > 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