> ...
> > > > even i want to save that form as it is (i mean, i want to save that
[quoted text clipped - 28 lines]
>
> Andrew T.
...
> thanks but i want to print the java components on paper..
Yes. Apparently you have two separate questions,
I provided one (of many) answers to the first, but was
making no attempt to answer the second one.
Did my 'answer 1' work for your 'question 1'?
(If there is only really one question - then I have a
major misunderstanding of your words - & the rest
below is not relevant..)
> how can i print it..
If I knew that, why do you think I would answer Q. 1,
but trim* all relevant information re. Q. 2?
(Go on - take a wild guess.)
* and as an aside, please trim text from replies that is
no longer relevant. As it was - all my words about saving
and restoring the state of a GUI are not relevant to your
need to print the (any) GUI.
Come to think of it, they are very different questions,
which would have been best put on separate threads. E.G.
"How to restore GUI?"
...as well as your first one..
"how to print the data with components?"
Andrew T.
> thanks but i want to print the java components on paper..
> how can i print it..
> how to use printable class?
from sun's forums, author of Standard/SpecialPrint is Tom Jacobs
I'd post a link, but I had to modify a couple of things to get it to work,
so easier to post the code.
(don't ask me how it works, I have no idea)
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.print.*;
import javax.print.PrintException;
import javax.swing.*;
import java.awt.event.*;
class Testing
{
public void buildGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JPanel p = new JPanel(new GridLayout(2,1));
p.add(new JLabel("Hello"));
JButton btn = new JButton("World");
p.add(btn);
final JFrame f = new JFrame();
f.getContentPane().add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
StandardPrint sp = new StandardPrint(f);//<----f is the component to print
try{sp.start();}catch(Exception e){}
}
});
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Testing().buildGUI();
}
});
}
}
class StandardPrint implements Printable, Pageable {
Component c;
SpecialPrint sp;
PageFormat mFormat;
boolean mScale = false;
boolean mMaintainRatio = true;
public StandardPrint(Component c) {
this.c = c;
if (c instanceof SpecialPrint) {
sp = (SpecialPrint)c;
}
}
public StandardPrint(SpecialPrint sp) {
this.sp = sp;
}
public boolean isPrintScaled () {
return mScale;
}
public void setPrintScaled(boolean b) {
mScale = b;
}
public boolean getMaintainsAspect() {
return mMaintainRatio;
}
public void setMaintainsAspect(boolean b) {
mMaintainRatio = b;
}
public void start() throws PrinterException {
PrinterJob job = PrinterJob.getPrinterJob();
if (mFormat == null) {
mFormat = job.defaultPage();
}
job.setPageable(this);
if (job.printDialog()) {
job.print();
}
}
public void setPageFormat (PageFormat pf) {
mFormat = pf;
}
public void printStandardComponent (Pageable p) throws PrinterException {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(p);
job.print();
}
private Dimension getJobSize() {
if (sp != null) {
return sp.getPrintSize();
}
else {
return c.getSize();
}
}
public static Image preview (int width, int height, Printable sp, PageFormat pf, int pageNo) {
BufferedImage im = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
return preview (im, sp, pf, pageNo);
}
public static Image preview (Image im, Printable sp, PageFormat pf, int pageNo) {
Graphics2D g = (Graphics2D) im.getGraphics();
int width = im.getWidth(null);
int height = im.getHeight(null);
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
double hratio = height / pf.getHeight();
double wratio = width / pf.getWidth();
//g.scale(hratio, wratio);
try {
sp.print(g, pf, pageNo);
}
catch(PrinterException pe) {
pe.printStackTrace();
}
g.dispose();
return im;
}
public int print(Graphics gr, PageFormat format, int pageNo) {
mFormat = format;
if (pageNo > getNumberOfPages()) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g = (Graphics2D) gr;
g.drawRect(0, 0, (int)format.getWidth(), (int)format.getHeight());
g.translate((int)format.getImageableX(), (int)format.getImageableY());
Dimension size = getJobSize();
if (!isPrintScaled()) {
int horizontal = getNumHorizontalPages();
int vertical = getNumVerticalPages();
int horizontalOffset = (int) ((pageNo % horizontal) * format.getImageableWidth());
int verticalOffset = (int) ((pageNo / vertical) * format.getImageableHeight());
double ratio = getScreenRatio();
g.scale(1 / ratio, 1 / ratio);
g.translate(-horizontalOffset, -verticalOffset);
if (sp != null) {
sp.printerPaint(g);
}
else {
c.paint(g);
}
g.translate(horizontal, vertical);
g.scale(ratio, ratio);
}
else {
double ratio = getScreenRatio();
g.scale(1 / ratio, 1 / ratio);
double xScale = 1.0;
double yScale = 1.0;
double wid;
double ht;
if (sp != null) {
wid = sp.getPrintSize().width;
ht = sp.getPrintSize().height;
}
else {
wid = c.getWidth();
ht = c.getHeight();
}
xScale = format.getImageableWidth() / wid;
yScale = format.getImageableHeight() / ht;
if (getMaintainsAspect()) {
xScale = yScale = Math.min(xScale, yScale);
}
g.scale(xScale, yScale);
if (sp != null) {
sp.printerPaint(g);
}
else {
c.paint(g);
}
g.scale(1 / xScale, 1 / yScale);
g.scale(ratio, ratio);
}
g.translate((int)-format.getImageableX(), (int)-format.getImageableY());
return Printable.PAGE_EXISTS;
}
public int getNumHorizontalPages() {
Dimension size = getJobSize();
int imWidth = (int)mFormat.getImageableWidth();
int pWidth = 1 + (int)(size.width / getScreenRatio() / imWidth) - (imWidth == size.width ? 1
: 0);
return pWidth;
}
private double getScreenRatio () {
double res = Toolkit.getDefaultToolkit().getScreenResolution();
double ratio = res / 72.0;
return ratio;
}
public int getNumVerticalPages() {
Dimension size = getJobSize();
int imHeight = (int)mFormat.getImageableHeight();
int pHeight = (int) (1 + (size.height / getScreenRatio() / imHeight)) - (imHeight ==
size.height ? 1 : 0);
return pHeight;
}
public int getNumberOfPages() {
if (isPrintScaled()) return 1;
return getNumHorizontalPages() * getNumVerticalPages();
}
public Printable getPrintable(int i) {
return this;
}
public PageFormat getPageFormat(int page) {
if (mFormat == null) {
PrinterJob job = PrinterJob.getPrinterJob();
mFormat = job.defaultPage();
}
return mFormat;
}
}
interface SpecialPrint {
public Dimension getPrintSize();
public void printerPaint(Graphics g);
}
ashwinijain - 29 Nov 2006 09:29 GMT
> I'd post a link, but I had to modify a couple of things to get it to work,
> so easier to post the code.
> (don't ask me how it works, I have no idea)
Thanks Michael.
Your code works.
I have one JPanel which is consisting of many swing components like
JTable, JList, JTextField,etc.
So, whenever i am passing that JPanel to constructor of StandardPrint,
multiple copies of Jtable is getting printed on many papers.
Please help me in solving this problem.
public void actionPerformed(ActionEvent e)
{
StandardPrint sp = new StandardPrint(b); // b is the instance of
JPanel.
try{sp.start();}catch(Exception x){}
}
Michael Dunn - 29 Nov 2006 21:23 GMT
> > I'd post a link, but I had to modify a couple of things to get it to work,
> > so easier to post the code.
[quoted text clipped - 14 lines]
> try{sp.start();}catch(Exception x){}
> }
works fine for me, printing a panel containing JTextArea, JTable,
JLabel, JButton.
one page only, showing all components and each one's text.
win98se, java1.4.0_01