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

voidremoveBadChars(JTable table, boolean skipHeaderBoolean)
remove Bad Chars
int columns = table.getColumnCount();
int rows = table.getRowCount();
int startRow = 0;
cleanHeaderCells(table);
startRow = 1;
for (int rCnt = startRow; rCnt < rows; ++rCnt) {
    for (int cCnt = 0; cCnt < columns; ++cCnt) {
        if (table.getValueAt(rCnt, cCnt) == null) {
...
voidsetHeaderIcon(JTable table, final int column, final ImageIcon icon)
set Header Icon
final TableCellRenderer orig = table.getTableHeader().getDefaultRenderer();
table.getTableHeader().getColumnModel().getColumn(column).setHeaderRenderer(new DefaultTableCellRenderer() {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {
        JLabel com = (JLabel) orig.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
                column);
        com.setHorizontalAlignment(JLabel.LEADING); 
...
voidsetOptimalHeaderWidth(int col)
sets the optimal header width for the given column.
setOptimalHeaderWidth(getJTable(), col);
voidsetPressedColumn(JTableHeader tableHeader, int columnModelIndex)
Sets the given column for the given JTableHeader as pressed.
tableHeader.putClientProperty(PRESSED_COLUMN_KEY, columnModelIndex);
voidsetTableHeaderCellRenderer(TableColumn tableCol)
set Table Header Cell Renderer
tableCol.setHeaderRenderer(new DefaultTableCellRenderer() {
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {
        if (table != null) {
            JComponent c = (JComponent) table.getTableHeader().getDefaultRenderer();
            setForeground(c.getForeground());
            setBackground(c.getBackground());
            setFont(c.getFont());
...
TableCellRenderersetupAsRowHeader(final JTable table)
Setups the given table for usage as row-header.
final JTableHeader header = table.getTableHeader();
Color background = header.getBackground();
Color foreground = header.getForeground();
if (background == null || background.equals(table.getBackground())) {
    if (!SystemColor.control.equals(background)) {
        background = SystemColor.control;
        foreground = SystemColor.controlText;
    } else {
...
voidsizeColumnsToFit(JTable table, boolean useHeader, boolean useContent)
size Columns To Fit
if (!useHeader && !useContent)
    return;
for (int i = 0; i < table.getColumnCount(); i++) {
    TableColumn column = table.getColumnModel().getColumn(i);
    int columnIndex = column.getModelIndex();
    int preferredWidth = 0;
    if (useContent) {
        for (int row = 0; row < table.getModel().getRowCount(); row++) {
...
String[]tableHeaders(JTable jtable)
Convert the table model column names to an array of strings.
TableModel tm = jtable.getModel();
List<String> colNameList = new ArrayList<>();
for (int i = 0; i < tm.getColumnCount(); i++) {
    colNameList.add(tm.getColumnName(i));
return colNameList.toArray(new String[colNameList.size()]);