flaimme
flaimme

Reputation: 99

JavaFx freezes after method is done

My problem is that my JavaFx application has gone to be very slow. Both in the start of the application and in some of the triggered events. It is a calendar application that uses a GridPane, that I am modifying. I have this method:

t.setOnMouseClicked(event->{

        long starttid = System.currentTimeMillis();
        System.out.println("start");
        if (markedTimeEnd != null && markedTimeStart != null) {
            colorMinutes(markedTimeStart, markedTimeEnd, Color.BLACK, bakrundWhite);
        } else if (markedTimeStart != null) {
            colorMinutes(markedTimeStart, markedTimeStart, Color.BLACK, bakrundWhite);
        }

        long tidNu = System.currentTimeMillis();
        long tid = tidNu-starttid;
        System.out.println("Print first time:\n"+tid);

        int minutTid = gridPane.getRowIndex(t);
        int timmeTimme = minutTid / 60;
        int minutMinut = minutTid - (60 * timmeTimme);
        markedTimeStart = new TidPunkt(timmeTimme, minutMinut);
        markedTimeEnd = null;

        tid = System.currentTimeMillis() -tidNu;
        tidNu = System.currentTimeMillis();
        System.out.println("Time for the middel calculations:\n"+tid);
        if (markedTimeEnd != null && markedTimeStart != null) {
            colorMinutes(markedTimeStart, markedTimeEnd, Color.GREEN,bakrundGren);
        } else if (markedTimeStart != null) {
            colorMinutes(markedTimeStart, markedTimeStart, Color.GREEN,bakrundGren);
        }
        event.consume();
        repaintAll();
        System.out.println("Time to end:\n"+(System.currentTimeMillis()-tidNu));
    });

And the code for the colorMinutes:

    private void colorMinutes(TidPunkt markedTimeStart, TidPunkt markedTimeEnd, Color colorText, Background colorOther) {

    System.out.println("The call is comming");

    int startBothTogether = markedTimeStart.getTimme() * 100 + markedTimeStart.getMinut();
    int endBothTogether = markedTimeEnd.getTimme() * 100 + markedTimeEnd.getMinut();

    System.out.println("Befor filter");
    gridPane.getChildren().stream()//parallelStream()
        .filter(x-> x.getId()!=null)
        .filter(y-> y.getId().matches("\\d\\d:\\d\\d"))
        .filter(pp->{
            int hoursForPart = Integer.parseInt(((Node) pp).getId().split(":")[0]);
            int miutesForPart = Integer.parseInt(((Node) pp).getId().split(":")[1]);
            int bothTogether = hoursForPart * 100 + miutesForPart;
            if (bothTogether >= startBothTogether && bothTogether <= endBothTogether)
                return true;
            else 
                return false;
        })
        .forEach(pp->{
            Platform.runLater(() -> {
                System.out.println("Changing collor ----");
                if(pp instanceof Pane){
                    ((Pane) pp).setBackground(colorOther)
                }else{
                    ((Text) pp).setFill(colorText);
                }
            });
        });
}

But it takes very long before it is change on the screen and it freezes sometime after what I can se is the finish of these methods by the System.out.println. I have been trying to profile but can't figure it out(best I have come up with it that is seems to be a big call tree, of javaFx "stuff" when exiting the method). The System.out.println prints is:

start
Print first time:
0
Time for the middel calculations:
0
The call is comming
Befor filter
Time to end:
373
Changing collor ----
Changing collor ----
Changing collor ----
Changing collor ----

But from button press to all is being color correct it takes many seconds.

The full code can be found here

Upvotes: 0

Views: 186

Answers (1)

fabian
fabian

Reputation: 82461

You're posting to many Runnables using Platform.runLater. There is no reason to use Platform.runLater here at all, since the onMouseClicked event handler is executed on the javafx application thread anyways.

Using

.forEach(pp->{
    if(pp instanceof Pane){
        ((Pane) pp).setBackground(colorOther)
    }else{
        ((Text) pp).setFill(colorText);
    }
});

Should improve the performance drastically.

Furthermore you seem to add a huge amout of Nodes in your repaintAll method (more specifically the ritaGrundKalender method) without removing Nodes which increases the number of Runnables for every click. I suggest you change this behavior.

Upvotes: 1

Related Questions