gpt4 book ai didi

java - 按下按钮时 ProgressIndicator 卡住。 JavaFX

转载 作者:行者123 更新时间:2023-11-29 09:06:19 30 4
gpt4 key购买 nike

我正在使用设置为 -1.0 的指示器进度来显示 loginprocess 运行时的一些加载。但是当我按下 Enter 按钮并使用 loginProcess 启动我的执行程序时,即使我使用 Plataform.runLater 设置可见,我的界面仍保持卡住我的 ProgressIndicator

我的按钮事件:

public void initManager(final LoginManager loginManager) {
btnEntrar.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {


String email = loginTxtField.getText().trim();
String token = tokenTxtField.getText().trim();

if (email.equals("")) {
Platform.runLater(new Runnable() {
public void run() {
Dialog.showError("Erro", "Digite o e-mail");
}
});
return;
}

try {

Future future = loginProcess(email, token);

showLoginLoading(future);

future.get();

if (!loginGatewayFailed && !loginTargetAppFailed) {

Login loginTargetApp = new Login(email, null, null);
loginManager.autheticated(loginTargetApp, loginGateway, gateway, file);

} else {

if (loginTargetAppFailed) {

Platform.runLater(new Runnable() {
public void run() {
Dialog.showError("Erro", loginTargetAppFailedCause);
}
});

} else {

if (loginGatewayFailed) {

Platform.runLater(new Runnable() {
public void run() {
Dialog.showError("Erro", loginGatewayFailedCause);
}
});
}
}
}

} catch (final Exception ex) {

Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, ex.getMessage());

Platform.runLater(new Runnable() {
public void run() {
Dialog.showError("Erro", ex.getMessage());
}
});
}
}
});
}

我的登录过程:

public Future<?> loginProcess(String email, String token) throws Exception {

// MY PROCESS

return Executors.newSingleThreadExecutor().submit(new LoginTask(this, email, token));

} catch (Exception e) {

Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, e.getMessage());

throw e;
}
}

方法显示登录加载:

private void showLoginLoading(Future future) {
while (!future.isDone()) {

Platform.runLater(new Runnable() {

@Override
public void run() {
progressInd.setVisible(true);
// progressInd.setProgress(-1.0);
}
});

}
}

最佳答案

问题出在线程管理上。我试图在主 FX View 运行的同一线程中执行登录指令。我想通了使用 Platform.isFxApplicationThread();如果调用线程是 JavaFX 应用程序线程,则返回 true。

为了解决我的问题,我只需要创建一个新线程来运行我的所有登录指令,如您在下面的示例中所见:

public void initManager(final LoginManager loginManager) {
btnEntrar.setOnAction(new EventHandler<ActionEvent>() {
boolean mainThread = Platform.isFxApplicationThread();
System.out.println("This is the main Thread: " + mainThread);

Platform.runLater(new Runnable() {

@Override
public void run() {
progressInd.setVisible(true);
}
});

new Thread() {
public void run() {

boolean mainThread = Platform.isFxApplicationThread();
System.out.println("This is the main Thread: " + mainThread);

String email = loginTxtField.getText().trim();
String token = tokenTxtField.getText().trim();

if (email.equals("")) {
Platform.runLater(new Runnable() {
public void run() {
Dialog.showError("Erro", "Digite o e-mail");
}
});
return;
}

try {

Future future = loginProcess(email, token);

// showLoginLoading(future);

future.get();

if (!loginGatewayFailed && !loginTargetAppFailed) {

Login loginTargetApp = new Login(email, null, null);
loginManager.autheticated(loginTargetApp, loginGateway, gateway, file);

} else {

if (loginTargetAppFailed) {

Platform.runLater(new Runnable() {
public void run() {
Dialog.showError("Erro", loginTargetAppFailedCause);
}
});

} else {

if (loginGatewayFailed) {

Platform.runLater(new Runnable() {
public void run() {
Dialog.showError("Erro", loginGatewayFailedCause);
}
});
}
}
}

} catch (final Exception ex) {

Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, ex.getMessage());

Platform.runLater(new Runnable() {
public void run() {
Dialog.showError("Erro", ex.getMessage());
}
});
}


}
}.start();
});
}

关于java - 按下按钮时 ProgressIndicator 卡住。 JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14878032/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com