Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Printing JFrame

  1. #1
    Junior Member spoX's Avatar
    Join Date
    May 2012
    Location
    Sweden
    Posts
    6
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Printing JFrame

    Hello.
    I have a problem to print my JFrame, i've read the tutorials A Basic Printing Program (The Java™ Tutorials > 2D Graphics > Printing)
    But i stil dont get it.

    When printing I get a new jframe,with a different look and like. Guess it has to do with new gui(), but "job.setPrintable(new gui(), pf)" won't take this as parameter?.


    Heres my code:

    public class gui extends JFrame implements Printable{
    ...
    ...
    ...
    	public gui(){
    	...
    	...
     
    		printAction.addActionListener( 
    				new ActionListener(){
    					public void actionPerformed(ActionEvent event){ 
     
    						PrinterJob job = PrinterJob.getPrinterJob();
    						PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    						PageFormat pf = job.pageDialog(aset);
    						job.setPrintable(new gui(), pf);
    						boolean ok = job.printDialog(aset);
    						if (ok) {
    							try {
    								 job.print(aset);
    							} catch (PrinterException ex) {
    							 /* The job did not successfully complete */
    							}
    						}
    					}
    				}
    		);
     
    		public int print(Graphics g, PageFormat pf, int page) throws PrinterException{
     
    			if (page > 0) {
    				return NO_SUCH_PAGE;
    			}
     
    			Graphics2D g2d = (Graphics2D)g;
    			g2d.translate(pf.getImageableX(), pf.getImageableY());
     
    			// Print the entire visible contents of a
    			// java.awt.Frame.
    			this.printAll(g);
     
    			return PAGE_EXISTS;
    		}
     
    	}
    }

    Sens.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,933
    Thanks
    55
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Printing JFrame

    Please post the full text of the error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member spoX's Avatar
    Join Date
    May 2012
    Location
    Sweden
    Posts
    6
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing JFrame

    Thanks for ur time Norm.

    I don' get an error message, when using the print I get a second jframe ontop of the first. And when print is finished i got 2 jframes.

    //

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,933
    Thanks
    55
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Printing JFrame

    won't take this as parameter?.
    I was referring to the above.

    Why are you creating a new gui object instead of using an existing one?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member spoX's Avatar
    Join Date
    May 2012
    Location
    Sweden
    Posts
    6
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing JFrame

    I treid, but don't know how to code it.
    "job.setPrintable(this, pf)" don't work?

    when using it i get this error in Eclips IDE

    "The method setPrintable(Printable, PageFormat) in the type PrinterJob is not applicable for the arguments (new ActionListener(){}, PageFormat)"
    Last edited by spoX; May 13th, 2012 at 07:35 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,933
    Thanks
    55
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Printing JFrame

    You need to refer to the gui class object, not the ActionListener object that is the inner class.
    Something like: gui.this
    Last edited by Norm; May 13th, 2012 at 07:50 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    spoX (May 13th, 2012)

  8. #7
    Junior Member spoX's Avatar
    Join Date
    May 2012
    Location
    Sweden
    Posts
    6
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing JFrame

    omg so simple.

    job.setPrintable(gui.this, pf);

    Thanks Norm u made my day.

    //

Similar Threads

  1. Replies: 1
    Last Post: September 28th, 2011, 08:29 AM
  2. using objects from one jFrame on a different jFrame
    By Taskeen in forum AWT / Java Swing
    Replies: 3
    Last Post: September 14th, 2011, 09:51 AM
  3. Printing Job
    By qwerty53 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 19th, 2011, 09:34 AM
  4. connecting jframe to another jframe
    By ajtambalo in forum Member Introductions
    Replies: 2
    Last Post: May 11th, 2011, 12:24 PM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 03:41 AM