• We just launched and are currently in beta. Join us as we build and grow the community.

Progress Bar in Java Swing library

zevohuhob

Tech Community Builder
Z Rep
0
0
0
Rep
0
Z Vouches
0
0
0
Vouches
0
Posts
153
Likes
78
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Some of the applications perform actions that can take a big amount of time. In this case you need to keep a connection with the user to show that your application doesn't stop to work or a failure happens. For this task a good way is to use a progress bar in your application.
Java Swing library provides a class that implements progress bar:
  1. public

    class

    JProgressBar


  2. extends

    JComponent


  3. implements

    SwingConstants

    , Accessible



To test JProgressBar class create new project with class ProgressBarDemo:
  1. import

    javax.swing.JFrame

    ;

  2. public

    class

    ProgressBarDemo extends

    JFrame

    {
  3. ProgressBarDemo(

    )

    {
  4. //size,title, exit
  5. setSize(

    400

    ,400

    )

    ;
  6. setTitle(

    "Progress Bar"

    )

    ;
  7. setDefaultCloseOperation(

    EXIT_ON_CLOSE)

    ;
  8. }
  9. public

    static

    void

    main(

    String

    [

    ]

    args)

    {

  10. }
  11. }

All the GUI elements will be defined in the constructor of the class. We will create a frame with 2 buttons and 2 types of progress bars.
To use JProgressBar class you need to add import of this class:
  1. import

    javax.swing.JProgressBar

    ;

To place all the GUI elements we will use a JPanel with BoxLayout:
  1. JPanel

    ProgressPanel =

    new

    JPanel

    (

    )

    ;
  2. ProgressPanel.setLayout

    (

    new

    BoxLayout

    (

    ProgressPanel, BoxLayout

    .Y_AXIS

    )

    )

    ;
  3. ProgressPanel.add

    (

    Box

    .createVerticalGlue

    (

    )

    )

    ;

A vertical glue is added to the top of the frame for better look of the application.
The first type of JProgressBar is an infinite

progress bar. To create an infinite progress bar you can use the following code:
  1. final

    JProgressBar

    infinite =

    new

    JProgressBar

    (

    )

    ;
  2. infinite.setIndeterminate

    (

    true

    )

    ;

Now we can add infinite

progress bar to the panel:
  1. ProgressPanel.add

    (

    infinite)

    ;
  2. ProgressPanel.add

    (

    Box

    .createVerticalGlue

    (

    )

    )

    ;

Another type of progress bar is a progress bar with percentage:
  1. final

    JProgressBar

    percentage =

    new

    JProgressBar

    (

    )

    ;

To show the progress of the action you need set PaintedString:
  1. percentage.setStringPainted

    (

    true

    )

    ;

For this progress bar we can set the range of values. We will use percentage, so the interval is set from 0 to 100:
  1. percentage.setMinimum

    (

    0

    )

    ;
  2. percentage.setMaximum

    (

    100

    )

    ;

Now we can add it to the panel:
  1. ProgressPanel.add

    (

    percentage)

    ;
  2. ProgressPanel.add

    (

    Box

    .createVerticalGlue

    (

    )

    )

    ;

To test progress bars work we will have a panel with buttons for different actions with progress bars:
  1. JPanel

    buttons =

    new

    JPanel

    (

    )

    ;
  2. buttons.setLayout

    (

    new

    BoxLayout

    (

    buttons, BoxLayout

    .X_AXIS

    )

    )

    ;
  3. buttons.add

    (

    Box

    .createHorizontalGlue

    (

    )

    )

    ;

Now we can add the buttons to this panel:
  1. JButton

    add =

    new

    JButton

    (

    "Add progress"

    )

    ;
  2. JButton

    infiniteBtn =

    new

    JButton

    (

    "Stop"

    )

    ;
  3. buttons.add

    (

    add)

    ;
  4. buttons.add

    (

    Box

    .createHorizontalGlue

    (

    )

    )

    ;
  5. buttons.add

    (

    infiniteBtn)

    ;
  6. buttons.add

    (

    Box

    .createHorizontalGlue

    (

    )

    )

    ;

The last step with GUI is to add buttons panel to the ProgressPanel:
  1. ProgressPanel.add

    (

    buttons)

    ;

And the ProgressPanel is added to the frame:
  1. this

    .getContentPane

    (

    )

    .add

    (

    ProgressPanel)

    ;

We need to manipulate progress bars. For this purpose we need to add actions listeners for both buttons. The first button Add progress

will increment the progress with 5 percents:
  1. add.addActionListener

    (

    new

    ActionListener

    (

    )

    {

  2. @Override
  3. public

    void

    actionPerformed(

    ActionEvent

    e)

    {
  4. int

    percents =

    percentage.getValue

    (

    )

    ;
  5. percents +=

    5

    ;
  6. if

    (

    percents <

    percentage.getMaximum

    (

    )

    )
  7. percentage.setValue

    (

    percents)

    ;
  8. else
  9. percentage.setValue

    (

    percentage.getMaximum

    (

    )

    )

    ;
  10. }

  11. }

    )

    ;

This Listener increment the value of the progress bar if it's possible.
Now we can write the listener for infinite progress bar that will stop the progress:
  1. infiniteBtn.addActionListener

    (

    new

    ActionListener

    (

    )

    {

  2. @Override
  3. public

    void

    actionPerformed(

    ActionEvent

    e)

    {
  4. infinite.setIndeterminate

    (

    false

    )

    ;
  5. infinite.setString

    (

    null

    )

    ;

  6. }

  7. }

    )

    ;

Now you know the basic operation with progress bars and you can keep the dialog with the user even if your application is performing a long term actions.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top