Raj*_*esh 34 multithreading javafx javafx-2 javafx-8
下面的代码片段给我错误Not on FX application thread; currentThread= JavaFX Application Thread.这个应用程序在java 1.7中工作得很好但是当我把它移动到fx8时它现在给出了错误.当我在第一次尝试时启动应用程序时它正在按预期工作.但是在关闭舞台并再次打开它之后它无法正常工作.
错误也是模棱两可的.Not On fx application thread and current thread- javafx application thread如果当前线程是fx应用程序线程,那么它不是在fx应用程序线程上意味着什么.
progressDialog = createProgressDialog(service);
progressDialog.show();
progressDialog.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
// if (service.isRunning()) {
// service.cancel();
progressDialog.close();
// }
}
});
}
@SuppressWarnings("unchecked")
private Stage createProgressDialog(final Service<IStatus> service) {
stage = new Stage();
URL url = FileLocator.find(Activator.getDefault().getBundle(),
new Path("icons/xxx_16x16.png"), null); //$NON-NLS-1$
stage.getIcons().add(new Image(url.getFile()));
stage.setTitle("Downloading ..."); //$NON-NLS-1$
// Creating StackPane
stage.initModality(Modality.WINDOW_MODAL);
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*fer 52
调用
Platform.runLater(new Runnable(){
// ...
});
Run Code Online (Sandbox Code Playgroud)
也会解决它.
vin*_*nay 38
当我从javafx 2中的任务修改UI元素时,就像listview元素一样,这发生在我身上.修改场景图的任务帮助我解决了问题,即更新UI元素
final ListView<String> group = new ListView ();
Task<Void> task = new Task<Void>() {
@Override protected Void call() throws Exception {
group.getItems().clear();
for (int i=0; i<100; i++) {
Platform.runLater(new Runnable() {
@Override public void run() {
group.getItems.add(i);
}
});
}
return null;
}
};
Run Code Online (Sandbox Code Playgroud)
art*_*rvt 19
当您尝试更改某些组件UI(如标签文本)时,应该会发生这种情况.像这样的运行总是:
@FXML Label myLabel;
Platform.runLater(new Runnable(){
myLabel.setText("some text");
});
Run Code Online (Sandbox Code Playgroud)
Raj*_*esh 16
Platform.setImplicitExit(false);解决了我的问题.我认为他们改变了JavaFX 8中的实现,因此在JavaFX 2中没有任何问题的相同代码在那里给出了不是fx应用程序线程错误.
您可以在代码的任何部分中更改Form或转到此视图或fxml:
Platform.runLater(() -> {
try {
Stage st = new Stage();
Parent sceneMain = FXMLLoader.load(getClass().getResource("/com/load/free/form/LoadFile.fxml"));
Scene scene = new Scene(sceneMain);
st.setScene(scene);
st.setMaximized(true);
st.setTitle("load");
st.show();
} catch (IOException ex) {
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
}
});
Run Code Online (Sandbox Code Playgroud)
我在控制器中的示例:
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class LoginController implements Initializable {
@FXML
private TextField txtUser;
@FXML
private TextField txtPassword;
@FXML
private Hyperlink urlForgetPassword;
@FXML
private Label lblError;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
public void isLoginAction(ActionEvent event) {
String message = "Ingrese ";
boolean isEmtpy = false;
if (txtUser.getText().trim().isEmpty()) {
message += "usuario y ";
isEmtpy = true;
}
if (txtPassword.getText().trim().isEmpty()) {
message += "contraseña ";
isEmtpy = true;
}
isEmtpy = false;
if (isEmtpy) {
message = message.substring(0, message.length() - 2);
lblError.getStyleClass().remove("message_process");
lblError.getStyleClass().add("message_error");
lblError.setText(message);
} else {
lblError.getStyleClass().add("message_process");
lblError.getStyleClass().remove("message_error");
Task task = new Task<Void>() {
@Override
protected Void call() throws Exception {
updateMessage("Procesando...");
System.out.println("Asignando DATOS DE PRUEBA ");
String passEnc = Encripta.encriptar(txtPassword.getText(), Encripta.HASH_SHA1);
int typeRest = new RestConnection().getConnectionUser(txtUser.getText(), passEnc);
if (typeRest == 1) {
//Load Another form
Platform.runLater(() -> {
try {
Stage st = new Stage();
Parent sceneMain = FXMLLoader.load(getClass().getResource("/com/load/free/form/LoadFile.fxml"));
Scene scene = new Scene(sceneMain);
st.setScene(scene);
st.setMaximized(true);
st.setTitle("");
st.show();
} catch (IOException ex) {
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
}
});
} else {
lblError.getStyleClass().remove("message_process");
lblError.getStyleClass().add("message_error");
updateMessage("Usuario y/o contraseña incorrectos");
}
return null;
}
};
lblError.textProperty().bind(task.messageProperty());
new Thread(task).start();
}
}
}
Run Code Online (Sandbox Code Playgroud)
它没有在上面的代码中明确显示,但我相当确定正在发生的事情是你在应用程序(主)javafx 线程之外的某个地方创建了一个线程,然后你试图对 javafx 对象执行操作(比如关闭,打开窗户等)在第二个线程上。这是绝对不允许的,因为只有主线程可以直接控制 javafx 对象。如果这成为您的程序的要求,您需要将第二个线程用于其他事情,例如计算等。您必须使用某种形式的消息传递让另一个线程知道您想要执行任何 javafx 操作。
| 归档时间: |
|
| 查看次数: |
70710 次 |
| 最近记录: |