jeff porter
jeff porter

Reputation: 6620

javafx doesnt repaint label till method has finished, why?

I have a JavaFX app with a some code like this...

public class MainListener extends EventListener{
    override public function event (arg0 : String) : Void {
     statusText.content = arg0;
    }
}

statusText is defined like this...

    var statusText = Text {
    x: 30
    y: stageHeight - 40
    font: Font { name: "Bitstream Vera Sans Bold" size: 10 }
    wrappingWidth: 420
    fill: Color.WHITE
    textAlignment: TextAlignment.CENTER
    content: "Status: awaiting DBF file."
};

I also have some other Javacode that is load data, much like this..

public ArrayList<CustomerRecord> read(EventListener listener) {

    ArrayList<CustomerRecord> listOfCustomerRecords = new ArrayList<CustomerRecord>();
        listener.event("Status: Starting read");

        // ** takes a while...
        List<Map<String, CustomerField>> customerRecords = new Reader(file).readData(listener);
        // ** long running method over.

        listener.event("Status: Loaded all customers, count:" + listOfCustomerRecords.size());
    return listOfCustomerRecords;
}

Now while the last method is in its long running call, I would expect to see my statusText updated to have 'Status: Starting read', but its doesn't. Its only when the read() method returns that the text is updated.

If its was 'straight' java I would presume that the long running job is hogging the CPU, or the statusText needed to have repaint() called on it.

Can anyone give me any ideas?

Thanks Jeff Porter

Upvotes: 0

Views: 626

Answers (1)

Honza
Honza

Reputation: 4409

JavaFX is single-threaded so anything you do on the main thread will block repaints. If you want to have repaints during a method run you need to run in on a separate thread. You can for example use javafx.async.JavaTaskBase for that.

Upvotes: 1

Related Questions