How to show real-time file progress in javafx?

I am working on the filetransfer module in my javafx application. So it works great, but when I wanted to show progress, it freezes until it finishes downloading. So I decided to use threads to train progress, but the console keeps telling me that I'm not on javafx thread, even if I use

Platform.runLater(new Runnable() { @Override public void run() { 

Here is my method that generates a popup that should show progress:

 public void generateFilePopUpMessage(final Stage primaryStage, final FileTransferRequest request) { try { popup.show(primaryStage); aceptar.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { class MyRunnable2 implements Runnable { public Label mensaje; /** * @return the mensaje */ public Label getMensaje() { return mensaje; } /** * @param mensaje the mensaje to set */ public void setMensaje(Label mensaje) { this.mensaje = mensaje; } @Override public void run() { // Accept it IncomingFileTransfer transfer = request.accept(); try { transfer.recieveFile(new File( "C:\\Users\\Raul\\" + transfer.getFileName())); while (!transfer.isDone()) { System.out.println("Progress: " + transfer.getProgress()); getMensaje().setText("getProgress: " + transfer.getProgress()); } if (transfer.getStatus().toString() .contains("Error")) { getMensaje().setText("Ocurrió un error al recibir el archivo."); } else if (transfer.getStatus().toString() .contains("Complete")) { getMensaje().setText("Archivo recibido correctamente."); } } catch (XMPPException ex) { ex.printStackTrace(); } finally { popup.hide(); } } } MyRunnable2 myRunnable2 = new MyRunnable2(); myRunnable2.setMensaje(mensaje); Thread myThread = new Thread(myRunnable2); myThread.setDaemon(true); // important, otherwise JVM does // not exit at end of main() myThread.start(); } }); rechazar.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { // Reject it try { request.reject(); } catch (Exception e) { } finally { popup.hide(); } } }); } catch (Exception e) { e.printStackTrace(); } } 

And here is the code where I call the popup:

 manager.addFileTransferListener(new FileTransferListener() { public void fileTransferRequest(final FileTransferRequest request) { // broadcast something here. Wheather users want to accept file // Check to see if the request should be accepted boolean accept = false; Platform.runLater(new Runnable() { @Override public void run() { popupFile.generateFilePopUpMessage(stage, request); popupFile.getUsuario().setText(request.getRequestor()); long size = bytesToMeg(request.getFileSize()); String tam = ""; if (size <= 0) { size = bytesToK(request.getFileSize()); tam = "" + size + "Kb"; } else { tam = "" + size + "Mb"; } popupFile.getMensaje().setText( "Le envía el siguiente archivo: " + request.getFileName() + ", que pesa: " + tam); } }); } }); 

How can i fix this ?? Thanks at advace.

0
source share

Source: https://habr.com/ru/post/976178/


All Articles