Swing Printing


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Swing Printing

  1. #1
    Join Date
    Apr 2005
    Posts
    12

    Unhappy Swing Printing

    For some reason when I try to print something my program freezes. If I shutdown the program then it will begin printing. I've looked through many of the tutorials, my code is the same as what they display. The class below is instantiated in another class and then the JPanel "ListingPanel" is added to either another panel or a frame. When a print button is hit it calls the printMe() method. Also when the program freezes it will cause the number of pages to be printed to go up until the application is shut down. e.g. if you look at the print manager for the printer, it says printing 1/100, 1/1264, and so on, the number will keep going up until the application is forcefully ended. Finally the 2d graphics object is not used because i removed it in testing, it didn't matter which one i used it still would freeze.


    Code:
    public class ListingPanel extends JPanel implements Printable
    {
    	public int print(Graphics g, PageFormat pf, int pi) throws PrinterException
    	{
    		//Graphics2D g2 = (Graphics2D) g;
    		g.translate((int)pf.getImageableX(), (int)pf.getImageableY());
    		Font f = new Font("Monospaced",Font.PLAIN,12);
    		g.setFont (f);
    		paint (g);
    		return Printable.PAGE_EXISTS;
    	}
     
    	public void printMe() 
    	{
    		PrinterJob printJob = PrinterJob.getPrinterJob();
    		printJob.setPrintable(this);
    		try 
    		{ 
    			printJob.print(); 
    		} 
    		catch (Exception PrintException) 
    		{ 
    			System.exit(34);
    		}
    	}
     
    }

  2. #2
    Join Date
    Jan 2005
    Posts
    45
    Hello,

    The first question that comes in mind is : have you try to run the print job in a thread ?

    When you run a long task in an listener (as it seems to be in your case), the event dispatching thread (the one that manages Gui) waits for the long task end !... The solution is simply to put the long task in the background, that means creating a new thread. So, in your listener code, you might try this:
    Code:
    (new Thread(){public void run(){myPanel.printMe();}}).start();
    Be careful yet, if printMe() updates gui during the print process, you must ensure that components will be updated through the event dispaching thread.

    You may find details about this here

    Hope that helps,
    Lionel Badiou
    CodeFutures -
    Java Code Generation

    http://www.codefutures.com



  3. #3
    Join Date
    Apr 2005
    Posts
    12

    thanks

    Thanks, I will try putting it into a thread. One question I have though, the printer can request that the GUI be painted at any time. It then takes the painted image and prints that. This can happen multiple times per page, so if I run it in the background and something in the GUI changes before the job finishes... would that effect the print? Any way I'll try it and see what happens, thanks again for your input.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center