?
Solved

How to Print JTable Completly

Posted on 2001-06-15
3
Medium Priority
?
368 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 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

764 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