This seems like a reasonable use case, and perhaps this was the original
intent of the "asynchronous call" documentation.
The problem though is that the play/stop code does not seem to take into
account being called from a different thread (there are several
synchronization issues when I delved into that code).
So then there's a choice to make I think, either:
- Disallow it completely, and have users wrap it into Platform.runLater()
- Have play/stop do the wrapping itself
- Make the methods thread safe by fixing the synchronization issues
--John
On 22/01/2024 11:59, Jurgen Doll wrote:
Here's an example as requested by Nir:
publicclassFxTimeLineTest extendsApplication
{
privateBorderPane bp= newBorderPane( newLabel("Loading") );
publicstaticvoidmain( String[] args) {
launch( FxTimeLineTest.class, args);
}
@Override
publicvoidstart( Stage primaryStage) throwsException {
newThread( newLoadScene() ).start();
primaryStage.setScene( newScene( bp, 300, 200 ) );
primaryStage.setTitle( "Memory Usage");
primaryStage.show();
}
privateclassLoadScene extendsTask<Parent> {
@OverrideprotectedParent call() throwsException {
Parent p= FXMLLoader.load( getClass().getResource("TestView.fxml") );
Thread.sleep( 1000 );
returnp;
}
@Overrideprotectedvoidsucceeded() {
bp.setCenter( getValue() );
}
@Overrideprotectedvoidfailed() {
getException().printStackTrace();
}
}
}
------------------------------------------------------------------------------------------------------
publicclassTestView
{
@FXMLprivateLabel memory;
privatestaticfinaldoubleMEGABYTE= 1024 * 1024;
@FXMLprivatevoidinitialize()
{
varupdater= newTimeline
(
newKeyFrame( Duration.seconds(2.5), event->
{
varruntime= Runtime.getRuntime();
doublemaxMemory= runtime.maxMemory() / MEGABYTE;
doubleusedMemory= (runtime.totalMemory() - runtime.freeMemory()) /
MEGABYTE;
memory.setText( (int) usedMemory+ " MB / "+ (int) maxMemory+" MB");
})
);
updater.setCycleCount(Animation.INDEFINITE); // This FXML is being
loaded on a background thread
updater.play();
}
}
------------------------------------------------------------------------------------------------------
TestView.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.StackPane?>
<StackPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="TestView">
<children>
<Label fx:id="memory" text="Current / Max MB" >
<properties hashCode="12345" />
</Label>
</children>
</StackPane>
On Sat, 20 Jan 2024 17:08:41 +0200, Nir Lisker <nlis...@gmail.com> wrote:
Hi Jurgen,
What I'm confused about the most is what it is you are actually
trying to do that necessitates the use of animations outside of
the FX thread. You said that you need to initialize controls on
another thread, and that you are using Task (both of which are
fine), but how does playing animations relate? Playing an
animation is something that is done explicitly, usually in order
to manipulate data. Can you give a real use case, like a minimized
version of what you're doing?
- Nir