JTable Right Align Header

Here’s an alternate approach to modifying the TableCellRenderer of a table’s JTableHeader. It’s not strictly necessary for this usage, but it minimizes the impact on the UI delegate’s appearance. Typical usage: JTable table = new JTable(…); JTableHeader header = table.getTableHeader(); header.setDefaultRenderer(new HeaderRenderer(table)); Custom header renderer: private static class HeaderRenderer implements TableCellRenderer { DefaultTableCellRenderer renderer; public … Read more

JTable Multiple Header Rows

We had the same requirement in our last project. What I have found is an Implementation for a GroupableTableHeader on java2s.com. However, I have pimped it a bit, although I cannot recall what exactly. Beneath is the implementation of the three classes as how we use them. ColumnGroup.java import java.awt.Component; import java.awt.Dimension; import java.util.ArrayList; import … Read more

How to merge cell in DefaultTableModel/JTable?

MultiSpanCellTableExample demonstrates how to merge cells by creating a custom TableUI. There seem to be a problem in this example that causes StackOverflowError, at least in Java 6. To fix this, inside AttributiveCellTableModel.setDataVector() replace: setColumnIdentifiers(columnNames); with: this.columnIdentifiers = columnNames; IE: public void setDataVector(Vector newData, Vector columnNames) { if (newData == null) throw new IllegalArgumentException( “setDataVector() … Read more

How can I put a control in the JTableHeader of a JTable?

The article How to Use Tables: Using Custom Renderers offers TableSorter as an example of how to detect mouse events on a column header. Using a similar approach, SelectAllHeader extends JToggleButton and implements TableCellRenderer in the example below to achieve a similar effect. A TableModelListener is used to condition the toggle button when all the … Read more