Java Utililty Methods JTable Header

List of utility methods to do JTable Header

Description

The list of methods to do JTable Header are organized into topic(s).

Method

ColorgetTableHeaderBackground()
get Table Header Background
return UIManager.getColor("TableHeader.background");
ColorgetTableHeaderColor()
get Table Header Color
return UIManager.getColor("TableHeader.background");
StringheaderString(JTable t)
header String
return headerString(t.getColumnModel());
booleanisColumnSelected(JTableHeader tableHeader, int columnModelIndex)
true if the given column model index is selected in the given JTableHeader .
int selectedColumn = getSelectedColumn(tableHeader);
return selectedColumn >= 0 && selectedColumn == columnModelIndex;
booleanisRowHeaderVisible(JTable table)
is Row Header Visible
Container p = table.getParent();
if (p instanceof JViewport) {
    Container gp = p.getParent();
    if (gp instanceof JScrollPane) {
        JScrollPane scrollPane = (JScrollPane) gp;
        JViewport rowHeaderViewPort = scrollPane.getRowHeader();
        if (rowHeaderViewPort != null)
            return rowHeaderViewPort.getView() != null;
...
voidlayoutHeaders(JTable p_Table)
layout Headers
int column = 0;
for (java.util.Enumeration columns = p_Table.getTableHeader().getColumnModel().getColumns(); columns
        .hasMoreElements(); column++) {
    TableColumn c = (TableColumn) columns.nextElement();
    Component component = c.getHeaderRenderer().getTableCellRendererComponent(p_Table, c.getHeaderValue(),
            false, false, -1, column);
    c.setPreferredWidth(Math.max(c.getPreferredWidth(), component.getPreferredSize().width));
voidmakeHeaderFillEmptySpace(JTable table)
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.
table.getTableHeader().setBorder(createTableHeaderEmptyColumnPainter(table));
voidmakeTableHeadersCentered(final JTable table, final boolean dataColsAlso)
Sets a JTable to have the headers be centered.
((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
        .setHorizontalAlignment(SwingConstants.CENTER);
if (dataColsAlso) {
    TableCellRenderer tcr = table.getDefaultRenderer(String.class);
    DefaultTableCellRenderer dtcr = (DefaultTableCellRenderer) tcr;
    dtcr.setHorizontalAlignment(SwingConstants.CENTER);
voidmodifyTableHeader(JTableHeader head, Vector columnNames)
Setzt den Header einer JTable
TableColumnModel m = head.getColumnModel();
if (m.getColumnCount() != columnNames.size()) {
    System.out.println("Column mismatch: " + m.getColumnCount() + "/" + columnNames.size());
    return;
for (int i = 0; i < columnNames.size(); i++) {
    TableColumn c = m.getColumn(i);
    c.sizeWidthToFit();
...
voidpaintHeader(Graphics graphics, JTable table, int x, int width)
Paints the given JTable's table default header background at given x for the given 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);