Create Pushable Button Table Headers In JTable

Swing components in Java are very flexible. You can change the table header of a JTable to become a JButton to display the headers as clickable. The class PushtableTableHeader does just that. See the code below.

To use the class, you can do it like this.

I purposely placed this in a class so that I could isolate only table columns that need to have a pushable button as its header.

Related Posts Plugin for WordPress, Blogger...

3 comments

  1. Awesome, thanks very much. Just one nit: if the columns are draggable (i.e. can be reordered by the user), you need to convert the column indexes to model indexes.

    In your code, line 23 should be

    renderer.setPressedColumn(header.getTable().convertColumnIndexToModel(col));

    And line 46 should be

    boolean isPressed = (table.convertColumnIndexToModel(column) == pushedColumn);

Leave a Reply

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