[prev in list] [next in list] [prev in thread] [next in thread]
List: openjdk-openjfx-dev
Subject: Psuedo-classes and weak references
From: "Adam Granger" <adam () adamish ! com>
Date: 2018-02-21 16:16:09
Message-ID: 3782c32bedc85549a560a7822ef9db06ec5b2ba9 () webmail ! adamish ! com
[Download RAW message or body]
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
);
}
});
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic