Solved

How to Print JTable Completly

Posted on 2001-06-15
3
358 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
use lov values 2 72
JavaFX TableView not displaying correctly 3 109
pagenation logic how it is working in my code 1 71
Java Inheritance super keyword use 8 40
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

730 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