This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Send to printer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Send to printer" Watch "Send to printer" New topic
Author

Send to printer

Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
I have this class file which I use to create new objects to display HTML documents. I would like to add a button to it. That button, when selected, would send the document to the local print host. Could I get some help doing this.

Thanks for taking a look.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Sure. What kind of help were you looking for? It helps if you ask a question, rather than just posting some code.

By the way why does that class extend JFrame, when it doesn't override any of JFrame's methods? Not only does it not pretend to be a JFrame, it even creates another JFrame. Extending JFrame looks to me to be unnecessary and confusing.
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
Ummm...It's above the code.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
start here

http://docs.oracle.com/javase/tutorial/uiswing/misc/printtext.html

if you get stuck, post what you've tried (basically what Paul was getting at --> what have you tried)
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
I was actually just looking at that page. I believe I can get the print function to work. The issue I'm having is getting a button to appear somewhere to click-to-print. Specifically, I'd like to have it appear up towards the title bar. Every attempt I have made, if I watch closely, the button does appear, but then gets covered by the html page.
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
@Paul Clapham - totally misunderstood your last post. Sorry about that.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> the button does appear, but then gets covered by the html page.

default layout for a JFrame (contentPane) is BorderLayout.
default position, if none specified, is BorderLayout.CENTER,
which occupies all available space.

looks like you're probably adding the button the same way as your scrollpane
htmlFrame.getContentPane().add(editorScrollPane);

if so, the button is not covered by the scrollpane, it is replaced in CENTER by the scrollpane

try it like this

htmlFrame.getContentPane().add(button,BorderLayout.NORTH);
htmlFrame.getContentPane().add(editorScrollPane,BorderLayout.CENTER);//specifying CENTER not really required

the button will occupy all of the top part of the frame (very wide), , so you might try adding the button
to a JPanel, then adding that panel to the frame at NORTH
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
Ok, so as an experiment, I am trying to build a panel with a button, and then add it to the frame, but I keep getting the following error:

C:\Users\LagerAndAle\Desktop\Java Sandbox\jpanel\CreateAJPanel.java:23: cannot find symbol
symbol : variable BorderLayout
location: class CreateAJPanel
panel.add(new JButton("print", BorderLayout.NORTH));
^
1 error

Tool completed with exit code 1


It works if I remove the BorderLayout.NORTH from the JButton? What am I doing wrong now?

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Scotty Steven wrote:It works if I remove the BorderLayout.NORTH from the JButton? What am I doing wrong now?


Attempting to invoke a nonexistent constructor for JButton.


luck, db
There are no new questions, but there may be new answers.
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
I forgot to import java.awt.BorderLayout;. opps!

Thanks.
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
This is where I am now. I have created the button and JPanel and attempted to flace it in a frame. Now, I can not see the HTML document.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
re-read my 2nd reply, then look closely
at how each of the components is added
to the frame.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

How to Use BorderLayout

BorderLayout supports only one component in each area. You've added two components to the default CENTER so only the last one added is seen.
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
I've been playing with this, with no success, from 5 hours now. I guess I'm going to need some more hints.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I guess I'm going to need some more hints.

look at the things added to the frame

htmlFrame.getContentPane().add(editorScrollPane);
htmlFrame.add(panel);

based on the information of earlier replies, what will happen to editorScrollPane?

later you add
panel.add(button, BorderLayout.NORTH);

so what should you see on the screen, when the frame is set to visible?
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
I understand that only one item can occupy a certain piece of space at any given time. What I'm not getting is how to divide the space up so that item a is at the North end, and Item b sets to the south of item a.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
might be simpler to show you (hopefully the light bulb will light up)

htmlFrame.getContentPane().add(editorScrollPane);//leave this line alone

//htmlFrame.add(panel);//change this line to the following
htmlFrame.add(panel,BorderLayout.NORTH);

//panel.add(button, BorderLayout.NORTH);//change this line to the following
panel.add(button);

basically you're adding:
the scrollpane to center - will occupy all available space
the button to the panel, so it will center in that panel, and not occupy all of the 'north' space
the panel to north - the rest will be taken up by the scrollpane at center
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
I kept missing that it was the button I was trying to send up north, not the panel. Dumb, dumb, dumb!

Anyways, I made the changes, but the results are still the same. Here is where I sit now:

Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
Nevermind. I had a pretty important line commented out.
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
I tried for the better part of two weeks (off and on) to get the printing to happen, but no luck. If anyone of the guru's could straighten out the code below with lots of comments to read and learn from, it would greatly be appreciated. I have added a button that, when pushed, I'm hoping would send the contents of the frame to the default print device, or bring up the printer dialog to select a print device.

Please and thank you.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You need to pass a Printable to the setPrinterJob method. You're now trying to pass a JScrollPane. That's not a Printable. The only Printable I see in your code is the HTMLFileDisplay class but you never use any instance of it.
Alternatively, JTextComponent has method getPrintable which can create a Printable for you. JEditorPane extends JTextComponent so it inherits this method.

On a side note, you should use the print(Graphics) method to print, not paint(Graphics). There are slight differences.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
I'm not sure what your post means. Went completely over my head.

I set this class up display one of 20 different html files that I have saves (file1). When called, the program receives the dimensions(x1, y1), a title (title1) and the file to load (html1). It is this html1 file that I wish to print when the print button is pressed.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Scotty Steven wrote:I'm not sure what your post means. Went completely over my head.

You are calling setPrintable on pJob.
setPrintable first argument must be an instance of a class that implements java.awt.print.Printable.
You are instead passing a reference to a JScrollPane, which does not implement java.awt.print.Printable.
As a result, you get a compiler error.

Apart from the editorScrollPane reference not being final and therefore not being accessible inside the anonymous inner class, it's simply not allowed to be used as an argument to setPrintable.

Your class itself, HTMLFileDisplay, does implement Printable. In other words, if you have an HTMLFileDisplay reference you can use that with setPrintable.

Alternatively, html.getPrintable(null, null) would return a Printable that you can use with setPrintable.
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
Thanks for your help. I have run out of time to get this done, so I just don't have time to experiment anymore. Unfortunate actually. I really would have this work.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Send to printer
 
Similar Threads
Reading HTML Files
enabling the scrollbar when components are dynamically added in jpanel
Creating a simple browser
JEditorPane and JScrollPane
problem in drawing lines on a panel