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.

This class auto resizes columns of a JTable, maximizing any non-used width columns and transferring those extra widths to other columns. This way, a column with a JCheckBox will have a width almost equal to that of a JCheckBox‘s width. The class TableColumnResizer contains one static method and you can pass the JTable object as the parameter. The code does the rest.

To use the method, do this:

Here is the code of the TableColumnResizer class.

Setting a JTable to display tooltip is easy. But what if a JTable cell’s text is not truncated? A tooltip should only appear whenever a cell text is truncated which is denoted by three dots …

To add this functionality in a JTable object, you would need to get the cell width and compare it with the text’s width based on its font by using the FontMetrics class. Place this code insider the getTableCellRendererComponent() method of a TableCellRenderer object.

There you go. The tooltip will only appear when your mouse cursor hovers over a JTable cell where the text is truncated.

Related Posts Plugin for WordPress, Blogger...