简体   繁体   中英

JavaFX - Progress Bar for Sorting Method

I have to create a JavaFX program that creates a list of randomly generated 5-digit integers, then use BubbleSort to sort them. I have to bind the sorting method to a progress bar to show it's status. How would I go about binding the progress bar to the BubbleSort method? (the size of the list can be varied to get the best results)

ProgressBars often don't measure real progress (because they sometimes can't know exactly how long something will really take to complete and exactly how far along in the process the progress currently is). If you can, then use real progress, otherwise use an estimate. In this case an estimate might be work better unless you can figure out some way to measure real progress of your bubble sort (I haven't tried to do that, and I don't know if you can, as pointed out by VGR in comments).

What you could do, is benchmark your sorting based upon different sized input sets. If the sort will be expected to take under a second for the size of input given, don't even bother showing a Progress Bar. If the sort is expected to take longer, when sorting actual data, calculate a time estimate based on your benchmark results for different input set sizes. Run a Timeline independent of the actual sort algorithm, which updates progress based upon your estimate. When actually done sorting, stop the timeline and set progress to 100%. It won't be perfect, but probably will be good enough.

I'll leave the implementation of this suggested strategy to an interested reader.

Another, simpler, solution is to use an indeterminate progress bar , which just spins until everything is complete. It isn't really showing progress at all, it just shows that something is (probably) happening. Sometimes that is an OK solution for some tasks, but for your sort progress measurement task, displaying a progress estimate or calculation of real progress might be preferred.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM