Solved

How to Print JTable Completly

Posted on 2001-06-15
3
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

724 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