ActionListener Events & PrinterJob

My Swing GUI will print one or more pages of photographs placed in a mug shot directory of multiple pages.  A user might request to print 2 copies of page 2, 4 copies of page 6, and 1 copy of page 8.  Then, they would input the page numbers to print, and number of copies for each page number, in text fields.  After that, they would click the Print button.  That is how I want it to work

I have included below the current print button action listener.  The problem I have is that the pj.printDialog pops up for every page to be printed.  

To make it easier to explain what I want, here's a scenario.  The user wants to...

* Print 3 copies of page 2.
* Print 4 copies of page 5.

Here are the events, as I would like them to transpire...

* User inputs text fields, specifying 3 copies of page 2, and 4 copies of page 5.
* User clicks Print button.
* Printer dialog box pops up.
* User selects printer.
* User clicks OK.

After that...

* 3 copies of page 2 printed.
* 4 copies of page 5 printed.

I am including below my current code.  How can I change it to operate as described above?
printButton.addActionListener(new ActionListener() {
	  public void actionPerformed(ActionEvent ae) {
	    int pgToPrint = 13; //This will be read from text field...
	    int numCopy=1; //this will be read from text field...
	    PrinterJob pj = PrinterJob.getPrinterJob();
	    ComponentPrinterFrame cpf = new ComponentPrinterFrame(pp,pgToPrint,arrPg);
	    if (pj.printDialog()) {
	      try { 
    		//Print page numCopy number of times...
    		for(int nCopy=0;nCopy<numCopy;nCopy++){
	      catch (PrinterException pe) {
	        pp.txtProgress.setText("Error: "+pe);

Open in new window

Who is Participating?
LarryAndroAuthor Commented:
Well, now that is an elegant answer!  :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.