【问题标题】:JavaFX UI Rendering Delay IssueJavaFX UI 渲染延迟问题
【发布时间】:2023-04-10 21:01:01
【问题描述】:

我有一个非常简单的 JavaFX 控制器,带有一个简单的用户名、密码和登录按钮。

我想要做的是当用户点击登录时,我想要禁用输入 - 我在代码中这样做:

this.gridPanelLogon.setDisabled(true);

而且-这可行,但我的问题是-它似乎是线程化的,在此调用之后,我对Web REST服务进行了JerseyClient调用-一旦该代码完成,然后更新UI并禁用gridPanel。但我想要的是 gridPanel 首先禁用 THEN 调用,并且似乎 UI 仅在所有代码运行后才更新(当它到达上面的代码行时不正确)。

如果我解释得不好,我深表歉意,我很乐意帮助澄清更多,但希望也许有人经历过这种情况,可以帮助解释原因或解决方法。我还尝试了另一种解决方法,将更改侦听器置于 gridPanel 的 disabled 属性 - 这不起作用并导致与上述相同的延迟。

任何帮助将不胜感激 - 非常感谢!!

【问题讨论】:

    标签: java user-interface javafx javafx-2 delay


    【解决方案1】:

    不要在 JavaFX 应用程序线程上运行客户端 => 服务器调用,而是通过 TaskService 在其 own thread 中运行它们。

    【讨论】:

    • 是的!再次感谢您 - 我也只是在尝试这个!你是对的 - 我发现 JavaFX 线程在一个线程上,我进行的客户端-> 服务器调用创建了一个新线程,JavaFX 在完成当前线程之前等待第二个线程完成。上面链接中的解决方案是Service - 再次感谢您周到及时的回复jewelsea!
    【解决方案2】:

    再次感谢 Jewelsea - 他很好地回答了许多 JavaFX 问题。我想分享这个解决方案,它在我的测试中非常适合我的应用程序。我正在发出 Jersey-Client REST 请求,并将其放在我的 JavaFX 应用程序中(没有创建扩展 javafx.concurrent.Service 的单独类)。

    所以我在下面所做的就是提供对我来说效果很好的解决方案,同时考虑到上面提供的链接 gemsea。成功 POST 到提供的 url 后,此 Service 类将返回一个 ClientResponse 对象。我已尝试在下面的 cmets 中提供更多关于此的说明。

    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;
                }
            };
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多