1
votes

I have a very simple JavaFX Controller with a simple Username, Password and Logon button.

What I want to do is when the user clicks Logon, I want the disable the inputs - which I do in code with:

this.gridPanelLogon.setDisabled(true);

And - this works but my issue is - it appears to be threaded, in that after this call I then make a JerseyClient call to a web REST service - and once that code completes it then updates the UI and disables the gridPanel. But what I would like is for the gridPanel to first disable THEN to call and it seems the UI is only updated after all the code runs (not right when it hits the line of code above).

If I explained this poorly I apologize, and I'd be happy to help clarify more but was hoping maybe someone has experienced this and could either help explain why or a work around. I also tried one other work around, putting a change listener to the gridPanel's disabled property - this didn't work and resulted in the same delay as mentioned above.

Any help would be greatly appreciated - and thanks!!

2

2 Answers

1
votes

Don't run client => server calls on the JavaFX application thread, instead run them in their own thread via a Task or Service.

0
votes

Thank you again to jewelsea - he answers many of these JavaFX questions so well. I wanted to share this solution, which in my testing works well for my application. I was making a Jersey-Client REST request, and was placing this inside my JavaFX application (without creating a separate class extending javafx.concurrent.Service).

So what I've done below is provide the solution that worked well for me, taking into account the links jewelsea provided above. This Service class will return a ClientResponse object after successfully POSTing to the url provided. I have tried to provide more notes about this in comments below.

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.concurrent.Service;
import javafx.concurrent.Task;

/**
 * This Service class will make REST calls to a given URL,
 * with a JSON encoded request string.
 *
 * OnSucceeded will return a ClientResponse object - will be
 * null if an exception occurred - if no exception is the
 * ClientResponse result of the Jersey-Client call.
 */
public class JerseyClientPostService extends Service<ClientResponse> {

    private StringProperty url = new SimpleStringProperty();
    private StringProperty json = new SimpleStringProperty();

    public JerseyClientPostService() {
        super();
    }

    public JerseyClientPostService(String url, String json) {
        super();
        this.url.set(url);
        this.json.set(json);
    }

    public final String getUrl() {
        return this.url.get();
    }

    public final String getJson() {
        return this.json.get();
    }

    public final void setJson(String json) {
        this.json.set(json);
    }

    public final void setUrl(String url) {
        this.url.set(url);
    }

    @Override protected Task<ClientResponse> createTask() {

        final String _url = getUrl();
        final String _json = getJson();

        return new Task<ClientResponse>() {
            @Override protected ClientResponse call() {
                Client client = Client.create();

                WebResource webResource = client.resource(_url);

                ClientResponse response;
                try {
                    response = webResource.type("application/json").post(ClientResponse.class, _json);
                }
                catch (ClientHandlerException che) {
                    System.err.println("ClientHandlerException connecting to Server: "+che.getMessage());
                    return null;
                }
                catch (Exception ex) {
                    System.err.println("Exception getting response Json: "+ex.getMessage());
                    return null;
                }
                return response;
            }
        };
    }
}