Solved

How to Print JTable Completly

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now