logo
down
shadow

Aligning messageformat on printing a JTable


Aligning messageformat on printing a JTable

By : KJP
Date : October 26 2020, 11:52 AM
Does that help You might look at this CustomTablePrintable. You feed it your table's unadorned getPrintable() result. In your PrinterJob, the custom print() will image the table and then draw your footer in the same graphics context. You can use the context's boundary, getFontMetrics() and stringWidth() to determine where to draw your formatted strings.
Addendum: Here's an example of printing a gray date in the bottom right corner of each page:
code :


Share : facebook icon twitter icon
MessageFormat header/footerFormat how to change Font for JTable printing

MessageFormat header/footerFormat how to change Font for JTable printing


By : user3118531
Date : March 29 2020, 07:55 AM
Does that help As everybody already mentioned (while I was relaxing in vacation :-) - TablePrintable is tightly knitted for secrecy, no way to subclass, no way to configure the header/footer printing. The only option to hook is to wrap the table's default printable, let it do its work without header/footer and take over the header/footer printing oneself.
The problem with the snippets shown so far is that they dont play nicely with multi-page - as known and mentioned by all authors, of course - because the default printable thinks there are no headers/footers and freely uses the space required by them. Not surprisingly :-)
code :
public class CustomPageFormat extends PageFormat {

    private PageFormat delegate;
    private double headerHeight;
    private double footerHeight;

    public CustomPageFormat(PageFormat format, double headerHeight, double footerHeight) {
        this.delegate = format;
        this.headerHeight = headerHeight;
        this.footerHeight = footerHeight;
    }
    /** 
     * @inherited <p>
     */
    @Override
    public double getImageableY() {
        return delegate.getImageableY() + headerHeight;
    }

    /** 
     * @inherited <p>
     */
    @Override
    public double getImageableHeight() {
        return delegate.getImageableHeight() - headerHeight - footerHeight;
    }

    // all other methods simply delegate
public class CustomTablePrintable implements Printable {

    Printable tablePrintable;
    JTable table;
    MessageFormat header; 
    MessageFormat footer;

    public CustomTablePrintable(MessageFormat header, MessageFormat footer) {
        this.header = header;
        this.footer = footer;
    }

    public void setTablePrintable(JTable table, Printable printable) {
        tablePrintable = printable;        
        this.table = table;
    }

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, 
            int pageIndex) throws PrinterException {
        // grab an untainted graphics
        Graphics2D g2d = (Graphics2D)graphics.create();
        // calculate the offsets and wrap the pageFormat
        double headerOffset = calculateHeaderHeight(g2d, pageIndex);
        CustomPageFormat wrappingPageFormat = new CustomPageFormat(pageFormat, headerOffset, 0);
        // feed the wrapped pageFormat into the default printable
        int exists = tablePrintable.print(graphics, wrappingPageFormat, pageIndex);
        if (exists != PAGE_EXISTS) {
            g2d.dispose();
            return exists;
        }
        // translate the graphics to the start of the original pageFormat and draw header
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        printHeader(g2d, pageIndex, (int) pageFormat.getImageableWidth());
        g2d.dispose();

        return PAGE_EXISTS;        
    }


    protected double calculateHeaderHeight(Graphics2D g, int pageIndex) {
        if (header == null) return 0;
        Object[] pageNumber = new Object[]{new Integer(pageIndex + 1)};
        String text = header.format(pageNumber);
        Font headerFont = table.getFont().deriveFont(Font.BOLD, 18f);
        g.setFont(headerFont);
        Rectangle2D rect = g.getFontMetrics().getStringBounds(text, g);
        return rect.getHeight();
    }

    protected void printHeader(Graphics2D g, int pageIndex, int imgWidth) {
        Object[] pageNumber = new Object[]{new Integer(pageIndex + 1)};
        String text = header.format(pageNumber);
        Font headerFont = table.getFont().deriveFont(Font.BOLD, 18f);
        g.setFont(headerFont);
        Rectangle2D rect = g.getFontMetrics().getStringBounds(text, g);
        // following is c&p from TablePrintable printText
        int tx;

        // if the text is small enough to fit, center it
        if (rect.getWidth() < imgWidth) {
            tx = (int) ((imgWidth - rect.getWidth()) / 2);

            // otherwise, if the table is LTR, ensure the left side of
            // the text shows; the right can be clipped
        } else if (table.getComponentOrientation().isLeftToRight()) {
            tx = 0;

            // otherwise, ensure the right side of the text shows
        } else {
            tx = -(int) (Math.ceil(rect.getWidth()) - imgWidth);
        }

        int ty = (int) Math.ceil(Math.abs(rect.getY()));
        g.setColor(Color.BLACK);
        g.drawString(text, tx, ty);

    }
}
    final JTable table = new JTable(myModel){

        /** 
         * @inherited <p>
         */
        @Override
        public Printable getPrintable(PrintMode printMode,
                MessageFormat headerFormat, MessageFormat footerFormat) {
            Printable printable = super.getPrintable(printMode, null, null);
            CustomTablePrintable custom = new CustomTablePrintable(headerFormat, footerFormat);
            custom.setTablePrintable(this, printable);
            return custom;
        }

    };
How to print multiple header lines with MessageFormat using a JTable

How to print multiple header lines with MessageFormat using a JTable


By : Raymond Garrison
Date : March 29 2020, 07:55 AM
will be helpful for those in need It's gonna be long answer (code wise) because the only solution I found was to implement a custom Printable. Of course I didn't write the following code myself, I mostly copied the code I extracted from the jdk sources and made some adjustments.
Here we are:
code :
DefaultTableModel dtm = new DefaultTableModel(new String[] { "Column 1" }, 1);

JTable table = new JTable(dtm) {
@Override
    public Printable getPrintable(PrintMode printMode, MessageFormat headerFormat, MessageFormat footerFormat) {
       return new TablePrintable(this, printMode, headerFormat, footerFormat);
    }
};
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 = 12.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("\n").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("\n");
            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("\n");
            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
                // the text shows; the right can be clipped
            } else if (table.getComponentOrientation().isLeftToRight()) {
                tx = 0;

                // otherwise, ensure the right side of the text shows
            } else {
                tx = -(int) (Math.ceil(rect.getWidth()) - imgWidth);
            }

            int ty = (int) Math.ceil(Math.abs(rect.getY() + i * rect.getHeight() / lines.length));
            g2d.drawString(lines[i], tx, ty);
        }
    }

    private void findNextClip(int pw, int ph) {
        final boolean ltr = table.getComponentOrientation().isLeftToRight();

        // if we're ready to start a new set of rows
        if (col == 0) {
            if (ltr) {
                // adjust clip to the left of the first column
                clip.x = 0;
            } else {
                // adjust clip to the right of the first column
                clip.x = totalColWidth;
            }

            // adjust clip to the top of the next set of rows
            clip.y += clip.height;

            // adjust clip width and height to be zero
            clip.width = 0;
            clip.height = 0;

            // fit as many rows as possible, and at least one
            int rowCount = table.getRowCount();
            int rowHeight = table.getRowHeight(row);
            do {
                clip.height += rowHeight;

                if (++row >= rowCount) {
                    break;
                }

                rowHeight = table.getRowHeight(row);
            } while (clip.height + rowHeight <= ph);
        }

        // we can short-circuit for JTable.PrintMode.FIT_WIDTH since
        // we'll always fit all columns on the page
        if (printMode == JTable.PrintMode.FIT_WIDTH) {
            clip.x = 0;
            clip.width = totalColWidth;
            return;
        }

        if (ltr) {
            // adjust clip to the left of the next set of columns
            clip.x += clip.width;
        }

        // adjust clip width to be zero
        clip.width = 0;

        // fit as many columns as possible, and at least one
        int colCount = table.getColumnCount();
        int colWidth = colModel.getColumn(col).getWidth();
        do {
            clip.width += colWidth;
            if (!ltr) {
                clip.x -= colWidth;
            }

            if (++col >= colCount) {
                // reset col to 0 to indicate we're finished all columns
                col = 0;

                break;
            }

            colWidth = colModel.getColumn(col).getWidth();
        } while (clip.width + colWidth <= pw);

    }
}
Printing 2 header lines from DataBase using MessageFormat to print a JTable?

Printing 2 header lines from DataBase using MessageFormat to print a JTable?


By : Aditya Paliwal
Date : March 29 2020, 07:55 AM
may help you . Unfortunately, I don't think there is a way to do that. jTable is calling upon AWT functions to draw the header and the footer. It uses FontMetrics.getStringBounds() to measure how much space the header (and footer) will take, and then uses Graphics.drawString() to draw it. Unfortunately, these two methods do not take newlines into account.
You may be able to do this by getting the Printable object from the JTable and wrapping it with your own implementation. But this is not trivial.
JTable Properly aligning inside JScrollPane

JTable Properly aligning inside JScrollPane


By : Suman Gandham
Date : March 29 2020, 07:55 AM
seems to work fine Understand that VPanel (which should be renamed vPanel to comply with Java naming rules) uses FlowLayout, and so the scrollpane may not fit well within it. Give it a BorderLayout and add the JScrollPane BorderLayout.CENTER if you want the scroll pane to fill it.
e.g.,
code :
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("serial")
public class TableFoo extends JPanel {
    private static final String[] HEADER = new String[] { "Country", "ID", "WAN IP", "User", "OS", "Java version" };
    private static final int PREF_W = 500;
    private static final int PREF_H = 400;
    private DefaultTableModel model = new DefaultTableModel(HEADER, 0);
    private JTable table = new JTable(model);

    public TableFoo() {
        for (int count = 0; count < 50; count++) {
            model.addRow(new Object[] { "data1", "data2", "data3", "data4", "data5", "data6" });
        }

        for (int count = 0; count < 70; count++) {
            model.addRow(new Object[] { "data100", "data200", "data300", "data400", "data500", "data600" });
        }

        JScrollPane scrollPane = new JScrollPane(table);

        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("TableFoo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new TableFoo());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
Change JTable Layout for Printing without affecting original JTable

Change JTable Layout for Printing without affecting original JTable


By : mdmilic
Date : March 29 2020, 07:55 AM
this one helps. Instead of cloning just create a copy with shared model. Below is working example. You can edit values by double-clicking. Changes are reflected in both tables. In your case you should use 'clone' with modified styles for printing.
code :
import javax.swing.*;
import java.awt.*;

public class CloningTablesExample {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(CloningTablesExample::runApp);
    }

    static void runApp(){
        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().setLayout(new GridLayout(2,1));
        window.setSize(400, 300);
        window.setVisible(true);

        JTable original = new JTable(new Object[][]{
                {"v1", "v2", "v3"},
                {"v4", "v5", "v6"}
            },
            new String[]{"col1", "col2", "col3"}
        );
        JTable clone = cloneTable(original);
        clone.setFont(clone.getFont().deriveFont(Font.BOLD));

        window.getContentPane().add(new JScrollPane(original));
        window.getContentPane().add(new JScrollPane(clone), BorderLayout.SOUTH);
    }

    private static JTable cloneTable(JTable original) {
        JTable clone = new JTable();
        clone.setModel(original.getModel());
        return clone;
    }
}
Related Posts Related Posts :
  • Android Spinner nullpointer
  • Add result to int array every time you finish counting the occurrence
  • Android import java library
  • How to use LDAP Authentication in a corporate environment
  • adding item during iteration in java special usecase
  • How can I sort a map with string key? like (1 foo , 2 foo)
  • How to test an implementation of TLS based on SSLEngine?
  • Sorting a Linked List in alphabetical order
  • Can't make more than one request on java.net.http.HttpClient or will receive: javax.net.ssl.SSLHandshakeException
  • Java logic - strange things happening in while loop
  • Unable to replicate an encryption method from Java to PHP using AES/ECB/PKCS5Padding
  • How program arithmetics on general objects which become defined only later?
  • Make asynchronous call synchronous in Kotlin
  • Thread Pool with Spring @Value for Pool size doesnt run properly
  • How to substring before nth occurence of a separator?
  • Resume S3 multipart upload: PartETag
  • Mutable fields should not be "public static"
  • How to remove fragment from URI
  • Add a node in xml using java only if the node has an attribute
  • java streams: elegant way to filter according an exception is thrown
  • Exception in thread, java code wont execute properly. If/else statments
  • using the same UI instance inside access method
  • Single Linked List in reverse order Java
  • Is there a java api that will identify the ipv6 address fd00:: as local/private?
  • Clear a cache with JSR-107 annotations
  • Store data from an array for later comparison
  • Why BufferedWriter here is not writing to the file even though I close it in the end?
  • Three methods to search: name; name and surname; and age in an array
  • When do I have to create new instance while using @Autowired
  • ArrayList public constructor - "Constructs an empty list with an initial capacity of ten" - where?
  • How to make assignable, number-like class in Java?
  • How to make a query in Firebase similar to a SQL query?
  • How to inflate SupportActionBar menu from outside Activity
  • Is it good practice using two navigation (component architecture) in the same app?
  • Can a method annotation handle errors thrown by this method?
  • How does the javadoc know which method each documentation comment is for?
  • Refactoring/moving java package between intellij projects
  • Why lists pksc11-tool seven Objects but Java Keystore has Only one
  • How to update an entity with an element-collection of type Map<String,String>?
  • How to solve a "1 producer 2 consumers" problem in java
  • java selenium - Nav to a page number not shown on a Paginated Table
  • Add dependecies to runtime image with Gradle
  • How to reload application.properties in runtime which is not part of jar
  • How to stream, in java, over a key->collection map, where each streamed element is the key and every element in the c
  • Android Listview - Load each row on thread
  • Vaadin: value from DateField is null after conversion
  • Payara 5 compatibility with Java 9
  • Deleting an item from RecyclerView + SQLite
  • Calling a function when an activity is returned to from fragment
  • Functions with different signatures, but the same body
  • How to check which installed JDK used during Gradle build process
  • Creating a reusable class
  • What does static in front of nested interface mean?
  • ByteBuffer Missing Data When decoded As string
  • How to remove Handler from adapter
  • Sort only even numbers in array and do not touch odds
  • Simple Todo android app with mlab dont add or edit
  • Type inference seems to fail vavr's Try works on jOOQ's fetchOne() function
  • Google Dataflow "No filesystem found for scheme gs"
  • Sort file based on date in file name
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org