Greetings, 

I've discovered the following code behaves differently in Java 8 vs 9,
in Java 8 it produces change and invalidation events, in Java 9
nothing.

I debugged this to the fact getPsuedoClassStates() returns a read-only
wrapper, and that uses weak references therefore there is nothing to
stop it garbage collected.

I'm able to work around this by storing getPsuedoClassStates() as a
field in my class but this feels horrible.

What is the recommended way of listening to changes in Psuedo states?

        public

class

PsuedoEvents

extends

Application

{

public

static

void
 main
(
String
[]
 args
)

{

 launch
(
args
);

}

@Override

public

void
 start
(
Stage
 stage
)

throws

Exception

{

Button
 horse 
=

new

Button
(
"horse"
);

 horse
.
getPseudoClassStates
().
addListener
(
new

InvalidationListener
()

{

@Override

public

void
 invalidated
(
Observable
 arg0
)

{

System
.
out
.
println
(
"invalidated "

+
 arg0
);

}

});

 horse
.
getPseudoClassStates
().
addListener
(
new

SetChangeListener
<>()

{

@Override

public

void
 onChanged
(
Change
<?

extends

PseudoClass
>
 arg0
)

{

System
.
out
.
println
(
"changed "

+
 arg0
);

}

});

 stage
.
setScene
(
new

Scene
(
new

HBox
(
horse
),

300
,

300
));

PseudoClass
 fooClass 
=

PseudoClass
.
getPseudoClass
(
"foo"
);

 horse
.
setOnAction
(
event 
->

{

 horse
.
pseudoClassStateChanged
(
fooClass
,

!
horse
.
getPseudoClassStates
().
contains
(
fooClass
));

System
.
out
.
println
(
"Toggle state... "

+
 horse
.
getPseudoClassStates
());

});

 stage
.
show
();

}

}

Work around

private

ObservableSet
<
PseudoClass
>
 states
;

// <=== strong ref

@Override

public

void
 start
(
Stage
 stage
)

throws

Exception

{

Button
 horse 
=

new

Button
(
"horse"
);

 states 
=
 horse
.
getPseudoClassStates
();

 states
.
addListener
(
new

InvalidationListener
()

{

@Override

public

void
 invalidated
(
Observable
 arg0
)

{

System
.
out
.
println
(
"invalidated "

+
 arg0
);

}

});


Reply via email to