Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Send to printer

 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20775
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummm...It's above the code.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul Clapham - totally misunderstood your last post. Sorry about that.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to import java.awt.BorderLayout;. opps!

Thanks.
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
re-read my 2nd reply, then look closely
at how each of the components is added
to the frame.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind. I had a pretty important line commented out.
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20497
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20497
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic