0

I want to print a JFrame with two things in it.

I have a Jtable that could have as many as 500 row in it, and I need to be able to print all of them. I have a JTextfield under the JTable in the Jframe. I want to print both of these at the same time. The problem I am having is when I print the jtable it only prints the visible part. What I really care about is the content of the JTable and not the Table itself.

Edit: I use Netbeans to build my Gui so I don't really know how to display the code for the Panel, Table, and TextFields.

                           PrinterJob PJ   =   PrinterJob.getPrinterJob();
                       PJ.setPrintable(this);
                    boolean doPrint = PJ.printDialog();

       JOptionPane.showMessageDialog(null, doPrint);
        if(doPrint){
            try {

                PJ.print();
            } catch (PrinterException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
           }
        } 

I have the print method here

   public int print(Graphics g, PageFormat format, int page_index) throws PrinterException {
    if (page_index > 0) {
        return Printable.NO_SUCH_PAGE;
    }
      // get the bounds of the component
    Dimension dim = this.getSize();
    double cHeight = dim.getHeight();
    double cWidth = dim.getWidth();

    // get the bounds of the printable area
    double pHeight = format.getImageableHeight();
    double pWidth = format.getImageableWidth();

    double pXStart = format.getImageableX();
    double pYStart = format.getImageableY();

    double xRatio = pWidth / cWidth;
   double yRatio = pHeight / cHeight;


    Graphics2D g2 = (Graphics2D) g;
    g2.translate(pXStart, pYStart);
    g2.scale(xRatio, yRatio);
    this.paint(g);
     this.print(g);
    return (Printable.PAGE_EXISTS);
}

now when i use this code it give me complete frame image and also only the dipalyed jtable..

1.What i want is how can omit the buttons

2.print my jlables ,jTextFields and my Jtable together on same page as it will be a bill of store... now when i m using jtable.print(); it is only showing jtable on page ..similar results for other JComponents any help ..how can i do this

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
ankit12
  • 5
  • 1
  • 8
  • Isolate the table and fields within their own panel/container, print this container – MadProgrammer Jan 03 '15 at 01:03
  • Any method to do that?? – ankit12 Jan 03 '15 at 01:08
  • [Printing table with scaling options](http://stackoverflow.com/questions/26660448/table-print-does-not-fit-to-page-size/26665276#26665276), [Printing a component with scaling options](http://stackoverflow.com/questions/22241711/setting-print-size-of-a-jlabel-and-put-a-jradiobutton-on-the-print/22242658#22242658) and [another example](http://stackoverflow.com/questions/22241711/setting-print-size-of-a-jlabel-and-put-a-jradiobutton-on-the-print/22244116#22244116) and [another example](http://stackoverflow.com/questions/12764634/printing-a-jframe-and-its-components/12765916#12765916) – MadProgrammer Jan 03 '15 at 01:12
  • My gut feeling is your going to need to do this by hand, determine the amount of space you have and scale the table to fit, printing the other components within the area they need – MadProgrammer Jan 03 '15 at 01:13
  • Or you could try using jasper reports, but I don't think you can restrict it to a single page – MadProgrammer Jan 03 '15 at 01:14
  • Thank u for the help..No i dont want to restrict it to one page ..but i want it should diplay fully ...with all Jlable n Jtextfields..as it is recipt – ankit12 Jan 03 '15 at 01:20
  • Do you have a picture of the layout requirements? I would suggest you have a close look at jasper reports, as it may fulfill your underlying requirements – MadProgrammer Jan 03 '15 at 01:28
  • what is jasper reports?? – ankit12 Jan 03 '15 at 02:00
  • It's an open source reporting framework which allows you to generate reports for printing, outputting to PDF, Word, Excel and other destinations...but I think you're only interested in the printing part – MadProgrammer Jan 03 '15 at 02:02
  • ya i have started reading about it just now ... it will take time to learn about it ...is't there is another method to do this ?? – ankit12 Jan 03 '15 at 02:11
  • Is there another way? Most of the time yes. You could create a offscreen view of you layou, using another container, creating new labels, textfields and your table, the problem here is, you won't be able to use a JScrollPane, so things get messy, as you lose the column header and will need to manage it yourself. You'd the size this component accordingly and print it – MadProgrammer Jan 03 '15 at 02:22
  • Instead of a using a JTextField, perhaps you could just pass its text as a footer to one of the JTable.print methods that accepts a header and footer, such as [this one](https://docs.oracle.com/javase/8/docs/api/javax/swing/JTable.html#print-javax.swing.JTable.PrintMode-java.text.MessageFormat-java.text.MessageFormat-)? – VGR Jan 04 '15 at 02:23

0 Answers0