| |

VerySource

 Forgot password?
 Register
Search
View: 793|Reply: 2

How to print a form?

[Copy link]

3

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-23 11:00:01
| Show all posts |Read mode
Print the data in the database according to a certain table format.
How to set the format, how to take the data and add it to the table, etc.
I hope the experienced prawns give pointers, thank you!
Reply

Use magic Report

1

Threads

51

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-5-20 15:30:01
| Show all posts
import javax.swing. *;
import javax.swing.table. *;
import java.awt.print. *;
import java.util. *;
import java.awt. *;
import java.awt.event. *;
import java.awt.geom. *;
import java.awt.Dimension;

public class Report implements Printable {
  JFrame frame;
  JTable tableView;

  public Report () {
    frame = new JFrame ("Sales Report");
    frame.addWindowListener (new WindowAdapter () {
    public void windowClosing (WindowEvent e) {
      System.exit (0);}});

    final String [] headers = {"Description", "open price",
        "latest price", "End Date", "Quantity"};
    final Object [] [] data = {
        {"Box of Biros", "1.00", "4.99", new Date (),
          new Integer (2)},
        {"Blue Biro", "0.10", "0.14", new Date (),
          new Integer (1)},
        {"legal pad", "1.00", "2.49", new Date (),
          new Integer (1)},
        {"tape", "1.00", "1.49", new Date (),
          new Integer (1)},
        {"stapler", "4.00", "4.49", new Date (),
          new Integer (1)},
        {"legal pad", "1.00", "2.29", new Date (),
          new Integer (5)},
          {"legal pad", "1.00", "2.29", new Date (),
          new Integer (5)},
          {"legal pad", "1.00", "2.29", new Date (),
          new Integer (5)},
          {"legal pad", "1.00", "2.29", new Date (),
          new Integer (5)},
          {"legal pad", "1.00", "2.29", new Date (),
          new Integer (5)},
          {"legal pad", "1.00", "2.29", new Date (),
          new Integer (5)},
          {"stapler", "4.00", "4.49", new Date (),
        new Integer (1)},
      {"legal pad", "1.00", "2.29", new Date (),
        new Integer (5)},
        {"legal pad", "1.00", "2.29", new Date (),
        new Integer (5)},
        {"legal pad", "1.00", "2.29", new Date (),
        new Integer (5)},
        {"legal pad", "1.00", "2.29", new Date (),
        new Integer (5)},
        {"legal pad", "1.00", "2.29", new Date (),
        new Integer (5)},
        {"legal pad", "1.00", "2.29", new Date (),
        new Integer (5)},
        {"stapler", "4.00", "4.49", new Date (),
        new Integer (1)},
      {"legal pad", "1.00", "2.29", new Date (),
        new Integer (5)},
        {"legal pad", "1.00", "2.29", new Date (),
        new Integer (5)},
        {"legal pad", "1.00", "2.29", new Date (),
        new Integer (5)},
        {"legal pad", "1.00", "2.29", new Date (),
        new Integer (5)},
        {"legal pad", "1.00", "2.29", new Date (),
        new Integer (5)},
        {"legal pad", "1.00", "2.29", new Date (),
        new Integer (5)}
      };

    TableModel dataModel = new AbstractTableModel () {
        public int getColumnCount () {
          return headers.length;}
        public int getRowCount () {return data.length;}
        public Object getValueAt (int row, int col) {
                return data [row] [col];}
        public String getColumnName (int column) {
                return headers [column];}
        public Class getColumnClass (int col) {
                return getValueAt (0, col) .getClass ();}
        public boolean isCellEditable (int row, int col) {
                return (col == 1);}
        public void setValueAt (Object aValue, int row,
                      int column) {
                data [row] [column] = aValue;
       }
     };

     tableView = new JTable (dataModel);
     JScrollPane scrollpane = new JScrollPane (tableView);

     scrollpane.setPreferredSize (new Dimension (500, 80));
     frame.getContentPane (). setLayout (
               new BorderLayout ());
     frame.getContentPane (). add (
               BorderLayout.CENTER, scrollpane);
     frame.setSize (400,200);
     JButton printButton = new JButton ();

     printButton.setText ("Print!");

     frame.getContentPane (). add (
               BorderLayout.SOUTH, printButton);


     printButton.addActionListener (new ActionListener () {
        public void actionPerformed (ActionEvent evt) {
          PrinterJob pj = PrinterJob.getPrinterJob ();
          pj.setPrintable (Report.this);
       // Use local dialog
          pj.printDialog ();
          try {
            pj.print ();
          } catch (Exception PrintException) {}
          }
        });

        frame.setVisible (true);
     }

     public int print (Graphics g, PageFormat pageFormat,
        int pageIndex) throws PrinterException {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor (Color.black);
        // Set the font
        int fontHeight = g2.getFontMetrics (). getHeight ();
        int fontDesent = g2.getFontMetrics (). getDescent ();

        // leave room for page number
        double pageHeight =
          pageFormat.getImageableHeight ()-fontHeight;
        double pageWidth =
          pageFormat.getImageableWidth ();
        double tableWidth = (double)
          tableView.getColumnModel (
          ) .getTotalColumnWidth ();
        double scale = 1;
        if (tableWidth> = pageWidth) {
                scale = pageWidth / tableWidth;
        }

        double headerHeightOnPage =
                 tableView.getTableHeader (
                 ) .getHeight () * scale;
        double tableWidthOnPage = tableWidth * scale;

        double oneRowHeight = (tableView.getRowHeight () +
                      tableView.getRowMargin ()) * scale;
        int numRowsOnAPage =
              (int) ((pageHeight-headerHeightOnPage) /
                                  oneRowHeight);
        double pageHeightForTable = oneRowHeight *
                                    numRowsOnAPage;
        int totalNumPages =
              (int) Math.ceil ((
                (double) tableView.getRowCount ()) /
                                    numRowsOnAPage);
        if (pageIndex> = totalNumPages) {
                      return NO_SUCH_PAGE;
        }

        g2.translate (pageFormat.getImageableX () + 10,
                       pageFormat.getImageableY ()-10);
       // Record the number of printed sheets
        g2.drawString ("Page:" + (pageIndex + 1) + "Copyright Asat",
            (int) pageWidth / 2-35, (int) (pageHeight
            + fontHeight-fontDesent));
        g2.translate (0f, headerHeightOnPage);
        g2.translate (0f, -pageIndex * pageHeightForTable);

        // If JTABLE is larger than the paper, print JTABLE smaller
        if (pageIndex + 1 == totalNumPages) {
           int lastRowPrinted =
                 numRowsOnAPage * pageIndex;
           int numRowsLeft =
                 tableView.getRowCount ()
                 -lastRowPrinted;
           g2.setClip (0,
             (int) (pageHeightForTable * pageIndex),
             (int) Math.ceil (tableWidthOnPage),
             (int) Math.ceil (oneRowHeight *
                               numRowsLeft));
        }
        // else clip to the entire area available.
        else {
             g2.setClip (0,
             (int) (pageHeightForTable * pageIndex),
             (int) Math.ceil (tableWidthOnPage),
             (int) Math.ceil (pageHeightForTable));
        }

        g2.scale (scale, scale);
        // Zoom operation
        tableView.paint (g2);
         // Print the body of JTABLE
        g2.scale (1 / scale, 1 / scale);
        g2.translate (0f, pageIndex * pageHeightForTable);
        g2.translate (0f, -headerHeightOnPage);
        g2.setClip (0, 0,
          (int) Math.ceil (tableWidthOnPage),
          (int) Math.ceil (headerHeightOnPage));
        g2.scale (scale, scale);
        tableView.getTableHeader (). paint (g2);
        // Print the header of JTABLE

        return Printable.PAGE_EXISTS;
        // PrinterJob uses Printable.PAGE_EXISTS to determine whether it still needs to print
   }

   public static void main (String [] args) {
       new Report ();

   }
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-2 10:00:02
| Show all posts
good stuff
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list