Solved

How to Print JTable Completly

Posted on 2001-06-15
3
352 Views
Last Modified: 2007-11-27
Dear Friends


I Created a JTable which shows me the report for a month.
i.e it contains 31 colums + 2 more colums.
when i get display i am getting it but they are being overlapped i.e the
colums are shrinking and i cant view the display. Offcourse i could
view it byu dragging . But when i am printing this table iam getting
the shrinked table only. I want the complete print out.
The container i have used is the max size the screen can accomidate.
so how to get the complete printout.
Please anyone help me.
I am in a great urgent.


bye
prasad

0
Comment
Question by:sdinfosys
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
kylar earned 100 total points
ID: 6196032
OK, the key here is that when you print your table, it will only print as much as the *table* thinks is viewable. So what you do is create the table, and put it in a scrollpane. Then when you call the print routine on it it will print the entire table, not just the visible portions. here is a sample "printable table" class. Create it, pop it in a scroll pane and when you want to call print, call doPrintActions().


                     public class PrintableTable extends JTable implements Printable{

                     /**
                      * The method @print@ must be implemented for @Printable@ interface.
                      * Parameters are supplied by system.
                      */
                         public int print(Graphics g, PageFormat pf, int pageIndex)
                                                            throws PrinterException {
                               Graphics2D g2 = (Graphics2D)g;
                               g2.setColor(Color.black);    //set default foreground color to black

                               RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
                               Dimension d = this.getSize();    //get size of document
                               double panelWidth  = d.width;    //width in pixels
                               double panelHeight = d.height;   //height in pixels
                               double pageHeight = pf.getImageableHeight();   //height of printer page
                               double pageWidth  = pf.getImageableWidth();    //width of printer page
                               double scale = pageWidth/panelWidth;
                               int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight);
                          // Make sure not print empty pages
                               if(pageIndex >= totalNumPages) {
                                return Printable.NO_SUCH_PAGE;
                               }

                          // Shift Graphic to line up with beginning of print-imageable region
                               g2.translate(pf.getImageableX(), pf.getImageableY());
                          // Shift Graphic to line up with beginning of next page to print
                               g2.translate(0f, -pageIndex*pageHeight);
                          // Scale the page so the width fits...
                               g2.scale(scale, scale);
                               this.paint(g2);   //repaint the page for printing
                               return Printable.PAGE_EXISTS;
                             }



                        public void doPrintActions(){
                            PrinterJob pj=PrinterJob.getPrinterJob();
                            pj.setPrintable(this);
                            pj.printDialog();
                            try{
                            pj.print();
                            }catch (Exception PrintException) {}
                        }

                     }

                     (Note that I didn't compile these, I just took them out of pieces of code I use. you may need to add
                     appropriate import statements, constructors etc..)

                     This printing technique can be applied to any component. The key is that it will only print as much
                     of the component as the component thinks exists. (that's why the JScrollPane is needed.)

If you are unclear or need any more help, let me know!
Cheers,
Kylar
0
 

Author Comment

by:sdinfosys
ID: 6197606
Dear Kylar


Thanks a lot for ur instant reply

I had user the
JScrollPane = new JScrollPane(table);

where table is my JTable.

I am getting vertical scroll bar but not horizontal.
what all the program u gave i am having it.
so please solve my problem.

it is very urgent.


bye
thanks in advance
prasad
0
 
LVL 4

Expert Comment

by:kylar
ID: 6202913
The problem is that a scrolled jtable will always re-size itself to fit in the horizontal area provided. You need to iterate through the columns and re-size them, and turn the JTable's auto-resize of columns off.

Kylar
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java JRE greater than 1.6 5 62
HTTPSessionBindingListernter not configured in deployment descriptor 2 53
eclipse apache tomcat admin console 52 97
null output 3 25
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question