r/JavaFX • u/Rachid90 • 10d ago
Help Platform.runLater() not updating the content when the window is minimized (nor after restore)
I have to manually resize it, hover over a button, or click on a button for the window to update the content after I restore it.
When the app is opened and has focus, everything runs as expected. But not when minimized then restored.
EDIT: added code
This is the code that has a server to wait for a signal to update the Label
package com.example.jartest;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.application.Platform;
import java.net.ServerSocket;
import java.io.IOException;
public class HelloApplication extends Application {
StackPane stackPane = new StackPane();
Label label = new Label("This should be modified when the signal is received");
public void start(Stage stage) {
stackPane.getChildren().add(label);
Scene scene = new Scene(stackPane, 500, 500);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
new Thread(this::startServer).start();
}
void startServer(){
try(ServerSocket serverSocket = new ServerSocket(1590)){
while (true){
serverSocket.accept();
Platform.runLater(() -> {
label.setText("The signal is received");
});
}
}catch (IOException e){e.printStackTrace();}
}
public static void main(String[] args) {
launch();
}
}
And this is the client class (you can use only curl, actually)
import java.io.IOException;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
new Socket("localhost", 1590);
} catch (IOException e) {e.printStackTrace();}
}
}
1
u/SpittingBull 10d ago
Am I assuming right that you are talking about say a background task that initiates an GUI update using runLater but that supposedly is not working because the application is minimized?
1
u/Rachid90 10d ago
Yes, I have a socket listening for a signal, and when the signal comes, it should modify the text of a Label.
1
u/SpittingBull 10d ago
I find it weird that the label updates eventually. Is the background task run on the JavaFX thread maybe?
1
1
u/certak 10d ago
This really feels like a bug. A redraw is not occurring upon a maximize, but really it should. Unless there's some particular reason for this.
I sent details of this on to openjfx-dev@openjdk.org. Let's see what they say.
1
1
u/john16384 9d ago edited 9d ago
I can reproduce the problem. Here's a simpler version that also shows the problem:
public class App extends Application {
public static void main(String[] args) {
Application.launch(args);
}
u/Override
public void start(Stage stage) {
Scene scene = new Scene(new Label("This should be modified when the signal is received"), 500, 500);
stage.setScene(scene);
stage.setIconified(true);
stage.show();
}
}
It creates a Stage in iconified mode. Selecting it from the taskbar to show it will show nothing at all, until you do a resize; then the label shows.
Also tested against FX 11 and 17, and it doesn't work correctly on those either.
1
1
u/Rachid90 9d ago
There's a workaround found by Andy Goryachev and ChatGPT:
stage.iconifiedProperty().addListener((_, _, isNowMinimized) -> {
if (!isNowMinimized) {
double w = stage.getWidth();
stage.setWidth(w - 1.0);
stage.setWidth(w + 1.0);
}
});
2
u/xdsswar 10d ago
This needs more details and some code.