Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to make my JavaFX application user interface (UI) responsive.

875776Dec 13 2012 — edited Dec 14 2012
I am trying to show the progress and the status of the each task in the JavaFX TableView. Each task represents by a row into a TableView. Each task executes in a separate Thread. Task may be like - downloading files from server, copy files from one account to another account etc. Please refer image. Every thing is working absolutely fine except JavaFX UI is freezing. May be because this muti-threaded environment.

As you can see in the image for Progress TableColumn I have set Cell Factory to render ProgressBar.
  
           TableColumn tableColumn_Progress = new TableColumn("Progress");
            tableColumn_Progress.setCellValueFactory(new PropertyValueFactory<QueueData, Double>("progressBar"));
            tableColumn_Progress.setPrefWidth(100);

            Callback<TableColumn<QueueData, Double>, TableCell<QueueData, Double>> attachmentCellFactory = //
                    new Callback<TableColumn<QueueData, Double>, TableCell<QueueData, Double>>() {
                @Override
                public TableCell call(final TableColumn param) {
                    final ProgressBarTableCell cell = new ProgressBarTableCell() {
                        @Override
                        public void updateItem(Object item, boolean empty) {
                            super.updateItem(item, empty);
                        }
                    };
                    return cell;
                }
            };
            tableColumn_Progress.setCellFactory(attachmentCellFactory); 
In QueueData Class -
    DoubleProperty progressBar = null;
    
    public DoubleProperty progressBarProperty() {
            return progressBar;
        }
    
        public Double getprogressBar() {
            return progressBar.get();
        }
    
        public void setprogressBar(Double sType) {
            this.progressBar = new SimpleDoubleProperty(sType);
        }
The TableView was not showing updated status and progress of tasks, so I am running a thread periodically in background, which modifies TableView when I call startUpdatingTableProgress() and stops when I call stopUpdatingTableProgress().
     public void stopUpdatingTableProgress() {
        keepUpdating = false;
    }

    public void startUpdatingTableProgress() {
        keepUpdating = true;
        TableProgressBarUpdator tpu = new TableProgressBarUpdator(table);
        tpu.start();
    }

    class TableProgressBarUpdator implements Runnable {

        TableView table;

        public TableProgressBarUpdator(TableView fxtable) {
            table = fxtable;

        }

        public void start() {
            new Thread(this).start();
        }

        public void run() {

            while (keepUpdating) {
                try {
                    updateProgressbar();
                    Thread.sleep(1000);
                } catch (Exception e) {
                    LogHandler.doErrorLogging("Error while updating tables cell", e);
                }
            }
            LogHandler.doDebugLogging("Table process repainting is completed.");
        }

        private void updateProgressbar() throws Exception {
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    ((TableColumn) table.getColumns().get(0)).setVisible(false);
                    ((TableColumn) table.getColumns().get(0)).setVisible(true);
                }
            });


        }
   }
But now My JavaFX app's UI freezes after consecutive times. Is there any alternative way to call updateProgressbar() in background thread that triggers automatically after specific time interval and do not freeze my `JavaFX` App's UI?



I do not know whether I am on the right way to update status of tasks where each task runs in a separate thread. If I would like to implement this with the help of `javafx.concurrent package` then can anybody show me some guidance or flow? What will go into the Task and what will go into Service Class? How I can schedule it to update progress bar? How to bind ProgressBar.progressProperty()?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 11 2013
Added on Dec 13 2012
2 comments
1,215 views