Showing progress bar when printing
Hi folks,
i have a problem here, when printing, i want to show a progress bar during the process. The Progress bar is placed on a JDialog.
an example :
publicint print(Graphics g, PageFormat pf,int page)throws PrinterException{
ProgressBarDialog d =new ProgressBarDialog();
d.setVisible(true);
while(printing not done){
d.updateProgressBar(int);// this method takes
// in a int and invokes JProgressBar's setValue(int) method to update the bar
// .. do print stuff here
}
d.setVisible(false);
}
however when i executed the code, i could see the dialog coming out, but in the contentPane it has no progressbar. any suggestions?
thanks!
Message was edited by:
TrAnScEnD3nT
# 1
first I don't agree with you in declaring the dialog in the method print because this method is called for each page (notice that it takes page number as argument).
So, you may declare the dialog and progress bar explicitly and update the progress in each call to print (or even, you may update the progress more than once in each call as you see suitable),
then your problem may occur because you do the printing in the EventDispatchThread (this will happen if you performed printing in actionPerformed() of a button, for example)
The EventDispatchThread is responsible also for updating the UI, so, if you perform printing in it, it will be busy in printing and have no time to update your UI, when printing is finished and the EventDispatchThread can update the UI, you will surely have removed the dialog.
if this is your case, try to make the printing occur in another thread
like this
public void actionPerformed(ActionEvent e)
{
new Thread()
{
public void run()
{
// do printing here
}
}.start();
}