Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2006

Tip: Looking for answers? Try searching our database.

how to print the data with components?

Thread view: 
ashwinijain - 27 Nov 2006 08:32 GMT
hello friends,
i have created one form which consists of jtable, jlist and many more
java components.
i want to print that form on printer..
even i want to save that form as it is (i mean, i want to save that
data with its GUI)...
how can i do these 2 things?
i dont know java very much...
please help me.....
Srik - 27 Nov 2006 08:56 GMT
you can just take the snapshot of it using 'print screen', paste it in
MS Paint and then take print.
And i dint understand your second requirement properly. You can always
save the contents in database or a file. when you start the gui again,
you can populate your table, list etc by reading from the database or
reading from file and parsing it.

> hello friends,
>  i have created one form which consists of jtable, jlist and many more
[quoted text clipped - 5 lines]
>  i dont know java very much...
>  please help me.....
ashwinijain - 27 Nov 2006 09:45 GMT
> you can just take the snapshot of it using 'print screen', paste it in
> MS Paint and then take print.
[quoted text clipped - 12 lines]
> >  i dont know java very much...
> >  please help me.....
actually i want to save my gui as it is..
i dont want to save it in database.
2nd problem is like i just wanted to print gui on printer
i  mean i want to create hard copy of my gui.
how can i print it?
Srik - 27 Nov 2006 10:17 GMT
Why do you mean by "saving the GUI as it is"? can you elaborate on your
requirement?  if its just the image of GUI you are talking about, you
can take the snapshot using ALT+PRINTSCREEN which takes the snapshot of
only the dialog u r displaying... then paste it on MS paint to take
'hard copy of your GUi' or save it.

> actually i want to save my gui as it is..
> i dont want to save it in database.
> 2nd problem is like i just wanted to print gui on printer
> i  mean i want to create hard copy of my gui.
> how can i print it?
Andrew Thompson - 27 Nov 2006 10:19 GMT
...
> > >  even i want to save that form as it is (i mean, i want to save that
> > > data with its GUI)...

There are a number of ways (and levels) of saving
and restoring the state of a GUI.

Here are some..

- implement serializable on the classes*, then
write/restore them as required.  That is fragile
across class version changes (breaks).
- If using 1.4+, use the XMLDecoder/Encoder
for the same basic thing.

* Although you might store/restore 'the frame' and
all its components, it might makes sense to store a
more limited form of the data, such as a CSV file
that is the data in the JTable.  If that is the case,
simply create a custom object to contain the
data of interest (or store the Vector that is the data,
etc.) - this can even help prevent the class version
changing when using serialisation.

- Use Properties(?) class
- Use the Web-Start API PersistenceService

The last two will only accept token/value String's, AFAIR.

HTH

Andrew T.
ashwinijain - 28 Nov 2006 06:06 GMT
> ...
> > > >  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..
how can i print it..
how to use printable class?
Andrew Thompson - 28 Nov 2006 06:52 GMT
...
> 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.
Michael Dunn - 28 Nov 2006 16:51 GMT
> 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
Ian Wilson - 28 Nov 2006 10:50 GMT
> hello friends,
>  i have created one form which consists of jtable, jlist and many more
> java components.
>  i want to print that form on printer..

http://java.sun.com/j2se/1.4.2/docs/guide/jps/index.html

>  even i want to save that form as it is (i mean, i want to save that
> data with its GUI)...

Others have answered that, I think.

>  how can i do these 2 things?
>  i dont know java very much...
>  please help me.....


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.