Search code examples
javaswingjprogressbar

Java swing indeterminate JProgressBar starting from the left when reaching end instead of bouncing


I created a JProgressBar in a GUI application, and setted it to "indeterminate", but I don't like that it bounces instead of restarting every time it reaches the end. What can I do to fix this graphic setting?


Solution

  • Change the UI of the JProgressBar.

    The UI is the class that paints the progress bar. The BasicProgressBarUI, makes the box bounce by default. You just have to write your own class extending MetalProgressBarUI (if you're using Metal) and overriding getBox(Rectangle), which is the method that stores the position of the box in the given rectangle.

    Use JProgressBar.setUI to apply the UI to your progress bar. You can also change the defaults with UIManager.put("ProgressBarUI", "fullyQualifiedClassName") to change the default UI for a progress bar.