JavaFX按钮句柄()不调用方法

问题描述:

这似乎是一个非常基本的问题,答案在我面前,但我仍然无法弄清楚什么是错的。我有一个按钮,处理单击事件时,我更改了标签的样式和文本。之后,我打电话给完成后再次改变风格的方法。JavaFX按钮句柄()不调用方法

我的问题是,handle()方法中的样式更改不会影响我的标签,而是直接从默认样式转换为connect()设置的样式。

请注意,这并不是因为它变化太快,connect()方法通常需要整整一秒才能完成,因为它连接到远程服务器。

我尝试睡觉线程一秒钟(如果我太慢)inbetween setStyle()和connect(),但无济于事。我将不胜感激任何帮助,并希望沿途学习。

这是我的代码:

Button loginButton = new Button(); 

    loginButton.setOnAction(new EventHandler<ActionEvent>() { 
     @Override 
     public void handle(ActionEvent event) { 
      loginStatus.setText("Loggin in..."); 

      //The line below should change the color until connect does it's thing, but it doesn't 
      loginStatus.setStyle("-fx-text-fill:#ffcc00"); 

      connect(username.getText(), password.getText(), serverField.getText()); 
     } 
    }); 

和connect()看起来是这样的:

private void connect(String username, String password, String server) { 
    try { 
     api = new DiscordBuilder(username, password).build(); 
     api.login(); 
     api.joinInviteId(server); 
     api.getEventManager().registerListener(new ChatListener(api)); 

     //Instead, it goes straight from the old style to the style set below. 
     loginStatus.setStyle("-fx-text-fill:#009933"); 

     loginStatus.setText("Online"); 
    } catch (NoLoginDetailsException e) { 
     loginStatus.setText("No login details!"); 
     loginStatus.setStyle("-fx-text-fill:#cc3300"); 
     e.printStackTrace(); 
    } catch (BadUsernamePasswordException e) { 
     loginStatus.setText("Bad username or password!"); 
     loginStatus.setStyle("-fx-text-fill:#cc3300"); 
     e.printStackTrace(); 
    } catch (DiscordFailedToConnectException e) { 
     loginStatus.setText("Failed to connect!"); 
     loginStatus.setStyle("-fx-text-fill:#cc3300"); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

你需要的是Task

也为表示here

JavaFX应用程序线程 上实现长时间运行的任务必然使得应用程序的用户界面反应迟钝。最佳做法是 在一个或多个后台线程上执行这些任务,并让JavaFX应用程序线程处理用户事件。

使你的代码看起来应该是这样

Button loginButton = new Button(); 
    loginButton.setOnAction(new EventHandler<ActionEvent>() { 
     @Override 
     public void handle(ActionEvent event) { 
      loginStatus.setText("Loggin in..."); 
      //The line below should change the color until connect does it's thing, but it doesn't 
      loginStatus.setStyle("-fx-text-fill:#ffcc00"); 
      Task<Void> task = new Task<Void>() { 
       @Override 
       protected Void call() throws Exception { 
        connect(username.getText(), password.getText(), serverField.getText()); 
        return null; 
       } 
      }; 
      new Thread(task).start(); 
     } 
    }); 

,并在你的连接方式与周围Platform.runLater

Platform.runLater(() -> { 
    loginStatus.setStyle("-fx-text-fill:#009933"); 
    loginStatus.setText("Online"); 
}) ; 
+0

谢谢你的UI更新方法!但是,在connect()方法中调用异常时,我遇到线程错误。如果没有抛出异常,那就没有问题了,因为我移动了“成功”结果的setStyle。但是,我将如何处理异常,而不会得到“线程中的异常”Thread-4“java.lang.IllegalStateException:不在FX应用程序线程上; currentThread = Thread-4”? –

+0

我改变了connect()返回一个布尔值,以便我可以将setStyles移出catch子句。它会停止线程异常,但不会告诉我抛出了什么样的异常。我觉得我正在接近解决方案,有什么想法? –

+0

好的,我编辑了我的答案,不要抛出关于在非FX应用程序线程中更新UI的例外... – guleryuz