Java JTable Header createTableHeaderEmptyColumnPainter(final JTable table)

Here you can find the source of createTableHeaderEmptyColumnPainter(final JTable table)

Description

Creates a Border that paints any empty space to the right of the last column header in the given JTable 's JTableHeader .

License

Open Source License

Declaration

private static Border createTableHeaderEmptyColumnPainter(final JTable table) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Component;
import java.awt.Graphics;
import javax.swing.CellRendererPane;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;

import javax.swing.table.TableCellRenderer;

public class Main {
    private static final CellRendererPane CELL_RENDER_PANE = new CellRendererPane();

    /**/*from  w w  w. j a  va2  s.c o  m*/
     * Creates a {@link Border} that paints any empty space to the right of the last column header
     * in the given {@link JTable}'s {@link JTableHeader}.
     */
    private static Border createTableHeaderEmptyColumnPainter(final JTable table) {
        return new AbstractBorder() {
            @Override
            public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
                // if this JTableHeader is parented in a JViewport, then paint the table header
                // background to the right of the last column if neccessary.
                JViewport viewport = (JViewport) table.getParent();
                if (viewport != null && table.getWidth() < viewport.getWidth()) {
                    int startX = table.getWidth();
                    int emptyColumnWidth = viewport.getWidth() - table.getWidth();
                    paintHeader(g, table, startX, emptyColumnWidth);
                }
            }
        };
    }

    /**
     * Paints the given JTable's table default header background at given
     * x for the given width.
     *
     * @param graphics the {@link Graphics} to paint into.
     * @param table    the table that the header belongs to.
     * @param x        the x coordinate of the table header.
     * @param width    the width of the table header.
     */
    public static void paintHeader(Graphics graphics, JTable table, int x, int width) {
        TableCellRenderer renderer = table.getTableHeader().getDefaultRenderer();
        Component component = renderer.getTableCellRendererComponent(table, "", false, false, -1,
                table.getColumnCount());

        component.setBounds(0, 0, width, table.getTableHeader().getHeight());

        ((JComponent) component).setOpaque(false);
        CELL_RENDER_PANE.paintComponent(graphics, component, null, x, 0, width, table.getTableHeader().getHeight(),
                true);
    }
}

Related

  1. AddToTable(JTable tbl, Object[] obj, boolean head)
  2. adjustHeader(JTable table)
  3. calcHeaderWidth(int col)
  4. calcHeaderWidth(JTable table, int col)
  5. cleanHeaderCells(JTable table)
  6. getColumnHeaderWidth(TableColumn col, JTable table)
  7. getHeaderDimension(JTable table, TableColumn tableColumn)
  8. getMaxColumnWidth(JTable aTable, int columnNo, boolean includeColumnHeaderWidth, int columnPadding)
  9. getSelectedColumn(JTableHeader tableHeader)