listview


Refreshing JavaFX ListView from ObservableList [duplicate]


I'd like to have a progress bar that updates inside an onAction block. For some reason in the following code, the progress bar doesn't update until it exits the onAction block. I get no progress for nine seconds and then 90%. The System.out prints just fine.
package net.snortum.play;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class ProgressBarTester extends Application {
public static void main( String[] args ) {
launch( ProgressBarTester.class, args );
}
#Override
public void start( final Stage stage ) throws Exception {
final GridPane grid = new GridPane();
grid.setAlignment( Pos.CENTER );
grid.setHgap( 10 );
grid.setVgap( 10 );
grid.setPadding( new Insets( 25, 25, 25, 25 ) );
final Text title = new Text( "Test Progress Bar" );
int col = 0, row = 0, colSpan = 2, rowSpan = 1;
grid.add( title, col, row, colSpan, rowSpan );
col = 0;
row++;
grid.add( new Label( "Progress:" ), col, row );
col = 1;
final ProgressBar progress = new ProgressBar( 0.0 );
grid.add( progress, col, row );
final HBox hbox = new HBox();
hbox.setAlignment( Pos.BASELINE_RIGHT );
final Button submit = new Button( "Go" );
hbox.getChildren().add( submit );
col = 1;
row++;
grid.add( hbox, col, row );
submit.setOnAction( new EventHandler<ActionEvent>() {
#Override
public void handle( ActionEvent event ) {
double size = 10.0;
for (double i = 0.0; i < size; i++){
progress.setProgress( i / size );
System.out.printf("Complete: %02.2f%n", i * 10);
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
} );
final Scene scene = new Scene( grid );
stage.setScene( scene );
stage.setTitle( "Test Progress Bar" );
stage.show();
}
}
Long form of tomsomtom's precise answer:
If you use the JavaFX Thread to do this kind of long term work, you stop the UI from updating etc. So you have to run your worker in a sparate thread. If you run in a separate Thread, all GUI operatione like progress.setProgress() have to be passed back to the JavaFX Thread using runLater() as JavaFX is not multithreaded.
Try this in your Action:
submit.setOnAction( new EventHandler<ActionEvent>() {
#Override
public void handle( ActionEvent event ) {
double size = 10.0;
new Thread(){
public void run() {
for (double i = 0.0; i < size; i++){
final double step = i;
Platform.runLater(() -> progress.setProgress( step / size ));
System.out.printf("Complete: %02.2f%n", i * 10);
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}.start();
}
} );
You are blocking the javafx event thread you need to do the work in another thread and sync back only the setProgress call with Platform.runLater

Related Links

Sencha Touch merging ListView back button bar with custom toolbar
understanding cellFactories and CallBack
how to set image to ImageView inside ListView in Xamarin?
Sencha Touch consecutive listviews double back buttons issue
Listview loaded twice
Different height in ListView
How to set selected item of ListView without scrolling the view
Can't update data from Kendo DataSource
Closed or circular Vertical ListView Android
How to set layout for topEmptyRegion in WearablListView?
How to disable groupedlist i.e. SemanticZoom+ListView virtualization in WP8.1 WinRT?
Listview item with NM_CUSTOMDRAW is flickering
How to stretch ListView items width to fill parent container?
POSH: Export Listview to CSV
Unable to receive all NM_CUSTOMDRAW dwDrawStage for a ListView
Custom ListView in SharePoint 2013 app

Categories

HOME
arduino-uno
openlayers
gitlab
fluentd
layout
appx
cplex
xmpp
youtube-api-v3
autotools
pheatmap
webrequest
metatrader4
izpack
circular-dependency
spring-tool-suite
moonmail
fallback
dynamics-crm-online
kryo
one-hot-encoding
zurb-foundation-6
wijmo
dxf
spark-jobserver
centos6.5
madlib
r-raster
fabric8
functor
kendo-ui-grid
caret
openoffice.org
jquery-bootgrid
janrain
android-mediaprojection
turbogears
apple-news
jsch
smartcontracts
termination
zip4j
jquery-nestable
ionicons
blogengine.net
svmlight
mplayer
colorama
pearson
gcsfuse
cubes
persist
hill-climbing
uid
xna-4.0
endeca-workbench
nsight
sonarqube5.1.2
system.reflection
camanjs
qpainter
google-reader
openexr
braille
monomac
shellexecute
qt-faststart
lcs
cassini-dev
e4x
pvrtc
trusted
gil
enter
gnustep
deobfuscation
horizontal-accordion
appender
self-extracting
propagation
sql-server-profiler
floating
icanhaz.js
project-hosting
nerddinner
private-members
uiq3

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App