Nero07
Nero07

Reputation: 23

Javafx project sometimes freezes on alert

I'm using Eclipse to write javafx code for my university courses. When I run the code either in Eclipse, or after exporting it as a jar file, The program sometimes freezes.

For Example:

Normal box for data entry:

I have this window, where values for a rectangle can be entered.

If you press "submit" without adding both values, a NumberFormatException occurs, which I catch like so:

        catch (NumberFormatException ex) {
        System.err.println("NumberFormatException: " + ex.getMessage());
        Alert alert = new Alert(AlertType.ERROR, "Bitte alle Felder ausfüllen", ButtonType.OK);
        alert.showAndWait();

The alert window sometimes appears and works fine. Sometimes (rarely) it does 10 times in a row. Other times I get this:

Error State:

The program is now frozen and nothing is clickable. There is a thin, black line through one of the textfields. The program never recovers and has to be manually terminated through Eclipse or the task manager.

This sometimes happens on the first click on submit. Sometimes on the 5th, or later. But it always eventually happens. The program works fine in all other instances, so long as this alert is not triggered too often.

I have also had it happen in another project, where the trigger was a new window for data entry opening, much like the one I have here, but this time, only the alert freezes the program. The data entry window can be opened as many times as I like.

I'm using (course mandated) JavaFX11 and JavaSE-11 in Eclipse, on Manjaro.

Minimal reproducible example:

package application;
    
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.GridPane;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            GridPane root = new GridPane();
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            Button submit = new Button("submit");
            Alert alert = new Alert(AlertType.ERROR, "Bitte alle Felder ausfüllen", ButtonType.OK);
            submit.setOnAction(e -> alert.show());
            root.add(submit,0,0);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

Opens a simple GridPane with just the submit button. If I click it, it sometimes shows the alert, but it often freezes as described above. I can see no pattern in the freezes. It's not always the first time, or always the second. Sometimes it's immediate, sometimes it works 3 or 4 times, then freezes on the next submit.

There is no error message in the console.

Edit: Further experimentation has shown, that the window does not freeze. If you hit return, it hits the button in the alert, which closes it and lets me use the rest of the program.

So it seems the error is in the window not being displayed correctly and only showing up as a thin black line. Which is still a problem, because it also happens with windows where I can't just hit enter to close them, which leaves me stuck.

Upvotes: 1

Views: 375

Answers (1)

Nero07
Nero07

Reputation: 23

Turns out it was the issue described here: https://github.com/javafxports/openjdk-jfx/issues/222

Plasma-KDE has a bug that prevents these kinds of windows from opening. There's a workaround posted if it's a one time thing.

I fixed it by switching to xfce.

Upvotes: 1

Related Questions