Java Reference
In-Depth Information
aJButton.addActionListener(actionListener);
frame.add(aJProgressBar, BorderLayout.NORTH);
frame.add(aJButton, BorderLayout.SOUTH);
frame.setSize(300, 200);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Figure 12-14 shows the demonstration program after selecting the button and at 22%
completion.
Figure 12-14. JProgressBar in action
By simply changing the sleep action in Listing 12-5 to the desired operation, this example
should provide a suitable framework for reuse.
Note To have the progress bar fill in the opposite direction, have the value start at the maximum and
decrease it with each step. You probably don't want to display the percentage-completed string, as it will start
at 100% and decrease to 0%.
Handling JProgressBar Events
Technically, the JProgressBar class supports notification of data model changes through a
ChangeListener . In addition, you can attach a ChangeListener to its data model. Because the
progress bar is meant more for visualization of output than for providing input, you typically
won't use a ChangeListener with it. However, there may be times when this is appropriate.
To reuse the BoundedChangeListener from Listing 12-3 earlier in this chapter, make one final
change (as shown in boldface in Listing 12-6), because the source of these change events is the
JProgressBar .
 
Search WWH ::




Custom Search