Java JTable Header makeHeaderFillEmptySpace(JTable table)

Here you can find the source of makeHeaderFillEmptySpace(JTable table)

Description

Installs a custom Border on the given table's JTableHeader that paints any blank area to the right of the last column header with the JTableHeader 's background.

License

Open Source License

Parameter

Parameter Description
table the JTable from which to get the JTableHeader to paint the empty column header space for.

Declaration

public static void makeHeaderFillEmptySpace(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. java 2s  .c  o  m*/
     * Installs a custom {@link Border} on the given table's {@link JTableHeader} that paints any
     * blank area to the right of the last column header with the {@code JTableHeader}'s background.
     *
     * @param table the {@link JTable} from which to get the {@code JTableHeader} to paint the
     *              empty column header space for.
     */
    public static void makeHeaderFillEmptySpace(JTable table) {
        table.getTableHeader().setBorder(createTableHeaderEmptyColumnPainter(table));
    }

    /**
     * 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. getTableHeaderColor()
  2. headerString(JTable t)
  3. isColumnSelected(JTableHeader tableHeader, int columnModelIndex)
  4. isRowHeaderVisible(JTable table)
  5. layoutHeaders(JTable p_Table)
  6. makeTableHeadersCentered(final JTable table, final boolean dataColsAlso)
  7. modifyTableHeader(JTableHeader head, Vector columnNames)
  8. paintHeader(Graphics graphics, JTable table, int x, int width)
  9. removeBadChars(JTable table, boolean skipHeaderBoolean)