Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So, I have a "print" button, that prints my jTable2 using a custom "print" function. I also use a "resize" function, to resize the columns for printing. But, I've tried to make the table fit the width of the page, but it doesn't do this, even if I change the PrintMode to FIT_WIDTH, or NORMAL, it has no difference between them. Also, I've tried changing the font size of the table, but it didn't work too. Here is the code:

package sistemabt;

import java.sql.*;

import java.util.Vector;

import javax.swing.JApplet;

import javax.swing.JOptionPane;

import javax.swing.table.DefaultTableModel;

import java.awt.*;

import java.awt.event.*;

import java.text.*;

import java.awt.print.*;

import javax.swing.JTable;

import javax.swing.table.TableColumn.*;

import javax.swing.table.JTableHeader;

import javax.swing.table.TableColumnModel;

import java.awt.geom.Rectangle2D;

import java.awt.geom.AffineTransform;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.print.PrinterException;

import java.util.Vector;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.JTableHeader;

import javax.swing.table.DefaultTableCellRenderer;

import javax.swing.table.JTableHeader;

import javax.swing.table.TableCellRenderer;

import javax.swing.table.TableColumn;

import java.awt.Component;

import java.awt.FontMetrics;

public class FormNotas extends javax.swing.JFrame {

private String driver = "org.postgresql.Driver";
private String banco = "bancoBT";
private String host = "localhost";
private String str_conn = "jdbc:postgresql://localhost:5432/" + banco;
private String usuario = "postgres";
private String senha = "root";

public static Connection con;
private Statement stmt;
private ResultSet rs;
public String sql;
public PreparedStatement pst;

static class TablePrintable implements Printable {

    private final JTable table;
    private final JTableHeader header;
    private final TableColumnModel colModel;
    private final int totalColWidth;
    private final JTable.PrintMode printMode;
    private final MessageFormat headerFormat;
    private final MessageFormat footerFormat;
    private int last = -1;
    private int row = 0;
    private int col = 0;
    private final Rectangle clip = new Rectangle(0, 0, 0, 0);
    private final Rectangle hclip = new Rectangle(0, 0, 0, 0);
    private final Rectangle tempRect = new Rectangle(0, 0, 0, 0);
    private static final int H_F_SPACE = 8;
    private static final float HEADER_FONT_SIZE = 18.0f;
    private static final float FOOTER_FONT_SIZE = 15.0f;
    private final Font headerFont;
    private final Font footerFont;

    public TablePrintable(JTable table, JTable.PrintMode printMode, MessageFormat headerFormat,
            MessageFormat footerFormat) {

        this.table = table;

        header = table.getTableHeader();
        colModel = table.getColumnModel();
        totalColWidth = colModel.getTotalColumnWidth();

        if (header != null) {
            // the header clip height can be set once since it's unchanging
            hclip.height = header.getHeight();
        }

        this.printMode = printMode;

        this.headerFormat = headerFormat;
        this.footerFormat = footerFormat;

        // derive the header and footer font from the table's font
        headerFont = table.getFont().deriveFont(Font.BOLD, HEADER_FONT_SIZE);
        footerFont = table.getFont().deriveFont(Font.PLAIN, FOOTER_FONT_SIZE);
    }

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {

        // for easy access to these values
        final int imgWidth = (int) pageFormat.getImageableWidth();
        final int imgHeight = (int) pageFormat.getImageableHeight();

        if (imgWidth <= 0) {
            throw new PrinterException("Width of printable area is too small.");
        }

        // to pass the page number when formatting the header and footer
        // text
        Object[] pageNumber = new Object[]{Integer.valueOf(pageIndex + 1)};

        // fetch the formatted header text, if any
        String headerText = null;
        if (headerFormat != null) {
            headerText = headerFormat.format(pageNumber);
        }

        // fetch the formatted footer text, if any
        String footerText = null;
        if (footerFormat != null) {
            footerText = footerFormat.format(pageNumber);
        }

        // to store the bounds of the header and footer text
        Rectangle2D hRect = null;
        Rectangle2D fRect = null;

        // the amount of vertical space needed for the header and footer
        // text
        int headerTextSpace = 0;
        int footerTextSpace = 0;

        // the amount of vertical space available for printing the table
        int availableSpace = imgHeight;

        // if there's header text, find out how much space is needed for it
        // and subtract that from the available space
        if (headerText != null) {
            graphics.setFont(headerFont);
            int nbLines = headerText.split("
").length;
            hRect = graphics.getFontMetrics().getStringBounds(headerText, graphics);

            hRect = new Rectangle2D.Double(hRect.getX(), Math.abs(hRect.getY()), hRect.getWidth(),
                    hRect.getHeight() * nbLines);

            headerTextSpace = (int) Math.ceil(hRect.getHeight() * nbLines);
            availableSpace -= headerTextSpace + H_F_SPACE;
        }

        // if there's footer text, find out how much space is needed for it
        // and subtract that from the available space
        if (footerText != null) {
            graphics.setFont(footerFont);
            fRect = graphics.getFontMetrics().getStringBounds(footerText, graphics);

            footerTextSpace = (int) Math.ceil(fRect.getHeight());
            availableSpace -= footerTextSpace + H_F_SPACE;
        }

        if (availableSpace <= 0) {
            throw new PrinterException("Height of printable area is too small.");
        }

        // depending on the print mode, we may need a scale factor to
        // fit the table's entire width on the page
        double sf = 1.0D;
        if (printMode == JTable.PrintMode.FIT_WIDTH && totalColWidth > imgWidth) {

            // if not, we would have thrown an acception previously
            assert imgWidth > 0;

            // it must be, according to the if-condition, since imgWidth > 0
            assert totalColWidth > 1;

            sf = (double) imgWidth / (double) totalColWidth;
        }

        // dictated by the previous two assertions
        assert sf > 0;

        // This is in a loop for two reasons:
        // First, it allows us to catch up in case we're called starting
        // with a non-zero pageIndex. Second, we know that we can be called
        // for the same page multiple times. The condition of this while
        // loop acts as a check, ensuring that we don't attempt to do the
        // calculations again when we are called subsequent times for the
        // same page.
        while (last < pageIndex) {
            // if we are finished all columns in all rows
            if (row >= table.getRowCount() && col == 0) {
                return NO_SUCH_PAGE;
            }

            // rather than multiplying every row and column by the scale
            // factor
            // in findNextClip, just pass a width and height that have
            // already
            // been divided by it
            int scaledWidth = (int) (imgWidth / sf);
            int scaledHeight = (int) ((availableSpace - hclip.height) / sf);

            // calculate the area of the table to be printed for this page
            findNextClip(scaledWidth, scaledHeight);

            last++;
        }

        // create a copy of the graphics so we don't affect the one given to
        // us
        Graphics2D g2d = (Graphics2D) graphics.create();

        // translate into the co-ordinate system of the pageFormat
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        // to save and store the transform
        AffineTransform oldTrans;

        // if there's footer text, print it at the bottom of the imageable
        // area
        if (footerText != null) {
            oldTrans = g2d.getTransform();

            g2d.translate(0, imgHeight - footerTextSpace);

            String[] lines = footerText.split("
");
            printText(g2d, lines, fRect, footerFont, imgWidth);

            g2d.setTransform(oldTrans);
        }

        // if there's header text, print it at the top of the imageable area
        // and then translate downwards
        if (headerText != null) {
            String[] lines = headerText.split("
");
            printText(g2d, lines, hRect, headerFont, imgWidth);

            g2d.translate(0, headerTextSpace + H_F_SPACE);
        }

        // constrain the table output to the available space
        tempRect.x = 0;
        tempRect.y = 0;
        tempRect.width = imgWidth;
        tempRect.height = availableSpace;
        g2d.clip(tempRect);

        // if we have a scale factor, scale the graphics object to fit
        // the entire width
        if (sf != 1.0D) {
            g2d.scale(sf, sf);

            // otherwise, ensure that the current portion of the table is
            // centered horizontally
        } else {
            int diff = (imgWidth - clip.width) / 2;
            g2d.translate(diff, 0);
        }

        // store the old transform and clip for later restoration
        oldTrans = g2d.getTransform();
        Shape oldClip = g2d.getClip();

        // if there's a table header, print the current section and
        // then translate downwards
        if (header != null) {
            hclip.x = clip.x;
            hclip.width = clip.width;

            g2d.translate(-hclip.x, 0);
            g2d.clip(hclip);
            header.print(g2d);

            // restore the original transform and clip
            g2d.setTransform(oldTrans);
            g2d.setClip(oldClip);

            // translate downwards
            g2d.translate(0, hclip.height);
        }

        // print the current section of the table
        g2d.translate(-clip.x, -clip.y);
        g2d.clip(clip);
        table.print(g2d);

        // restore the original transform and clip
        g2d.setTransform(oldTrans);
        g2d.setClip(oldClip);

        // draw a box around the table
        g2d.setColor(Color.BLACK);
        g2d.drawRect(0, 0, clip.width, hclip.height + clip.height);

        // dispose the graphics copy
        g2d.dispose();

        return PAGE_EXISTS;
    }

    private void printText(Graphics2D g2d, String[] lines, Rectangle2D rect, Font font, int imgWidth) {

        g2d.setColor(Color.BLACK);
        g2d.setFont(font);

        for (int i = 0; i < lines.length; i++) {
            int tx;

            // if the text is small enough to fit, center it
            if (rect.getWidth() < imgWidth) {
                tx = (int) (imgWidth / 2 - g2d.getFontMetrics().getStringBounds(lines[i], g2d).getWidth() / 2);

                // otherwise, if the table is LTR, ensure the left side of
                // t

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
95 views
Welcome To Ask or Share your Answers For Others

1 Answer

There are two possible options you have, you could try and resize the columns so that are evenly distributed across the available page width OR you could scale the resulting output UP so it fits the page.

Scaling

By default, the TablePrintable only scales DOWN, forcing a JTable that is too large to fit within the available page size (width). You can change this to allow it to also scale UP.

The piece of code that calculates the scale is within the print of the TablePrintable class and looks like...

double sf = 1.0D;
if (printMode == JTable.PrintMode.FIT_WIDTH && totalColWidth > imgWidth) {

    // if not, we would have thrown an acception previously
    assert imgWidth > 0;

    // it must be, according to the if-condition, since imgWidth > 0
    assert totalColWidth > 1;

    sf = (double) imgWidth / (double) totalColWidth;
}

The part we're interested in is the if statement, which reads "if printmode equals FIT_WIDTH AND the totalColWidth is greater the the page width"...We want to change this to read ""if printmode equals FIT_WIDTH" only...

You could change

if (printMode == JTable.PrintMode.FIT_WIDTH && totalColWidth > imgWidth) {

to

if (printMode == JTable.PrintMode.FIT_WIDTH) {

which will now allow the TablePrintable to both scale the table UP and DOWN...

Which will result in something like...

Result

  • Top is the screen output
  • Left is the current result
  • Right is the scaled result

Resize Columns

This is a little more tricky and SHOULD never be applied to a JTable that is already on the screen, as this will mess with how it is actually displayed...

Basically, when the table is printed, we're going to override the column widths to give them equal space across the page...

First, we need to change totalColWidth in TablePrintable from...

private final int totalColWidth;

to

private int totalColWidth;

because we need to be able to modify the value after it's initialised...

Next, we need a flag to determine if the columns have been modified or not, as it's a waste to have to repeatedly update their sizes every time print is called.

Add private boolean updateColumnWidths; to the fields of TablePrintable (for example, under private final Font footerFont;)

Now, when print is called, we need to make a series of decisions...

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {

    // for easy access to these values
    final int imgWidth = (int) pageFormat.getImageableWidth();
    final int imgHeight = (int) pageFormat.getImageableHeight();

    if (imgWidth <= 0) {
        throw new PrinterException("Width of printable area is too small.");
    }

    // Have we modified the column widths yet??
    if (!updateColumnWidths) {

        // Only update the column widths if the current total column width
        // is less then the available imgWidth (page width)
        if (totalColWidth < imgWidth) {

            // Calculate the required column width to allow the columns to
            // span the page...
            int columnCount = table.getColumnCount();
            int columnWidth = (int) (imgWidth / (float) columnCount);
            TableColumnModel columnModel = table.getColumnModel();
            // Update the columns...
            for (int col = 0; col < columnModel.getColumnCount(); col++) {
                TableColumn tc = columnModel.getColumn(col);
                tc.setMinWidth(columnWidth);
                tc.setMaxWidth(columnWidth);
                tc.setPreferredWidth(columnWidth);
                tc.setWidth(columnWidth);
            }
            // Update the totalColWidth, this should prevent
            // any scaling been applied
            totalColWidth = columnModel.getTotalColumnWidth();
            
        }
        updateColumnWidths = true;

    }
    //...

Which generates something like...

Resized


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...