Hide JTable Column Headers

Hiding all column headers of a JTable is pretty easy. You just need to set to null the method setTableHeader(null).

A very common problem is that once you call this method, nothing happens. The column headers still get displayed. While other workarounds involve setting column header labels to blank, you would still need column names to get access to the column object through their column names (I prefer this way compared to accessing it via a column’s index number directly).

The JTable method setTableHeader() when set to null is only good if there is no JScrollPane on it. Chances are there is one attached to your JTable object because it is needed when you have many rows to consider. You need to call a second method from the JScrollPane in order for the column headers to be hidden. Call the setColumnHeaderView() method and set it to null.

That should do the trick.

Related Posts Plugin for WordPress, Blogger...

3 comments

  1. Hi ladies,

    note that if you set the header null, then the auto resize mode is no longer respected. Try the following:

    if (table.getTableHeader().isVisible() != visible) {
    table.getTableHeader().setVisible(visible);
    table.getTableHeader().setPreferredSize(visible ? null : new Dimension(-1, 0));
    }

Leave a Reply

Your email address will not be published. Required fields are marked *