Table Print does not fit to page size

眉间皱痕 提交于 2019-11-27 16:30:37

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...

  • 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...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!