tags:

views:

1111

answers:

7

What is the best way to put a footer row into a JTable? Does anyone have any sample code to do this?

The only approach I've thought of so far is to put a special row into the table model that always get sorted to the bottom.


Here is what I ended up with:

JTable mainTable = new JTable(mainTableModel);
JTable footerTable = new JTable(footerModel);
footerTable.setColumnModel(mainTable.getColumnModel());

// Disable selection in the footer. Otherwise you can select the footer row
// along with a row in the table and that can look quite strange.
footerTable.setRowSelectionAllowed(false);
footerTable.setColumnSelectionAllowed(false);

JPanel tablePanel = new JPanel();
BoxLayout boxLayout = new BoxLayout(tablePanel, BoxLayout.Y_AXIS);
tablePanel.setLayout(boxLayout);
tablePanel.add(mainTable.getTableHeader()); // This seems like a bit of a WTF
tablePanel.add(mainTable);
tablePanel.add(footerTable);

Sorting works fine but selecting the footer row is a bit strange.

A: 

The only time I have done this I just added a row in the model like so:

 @Override
 public int getRowCount() {
  return _tableContents.size() + 1;
 }

_tableContents is of course the actual data behind my model. You'll have to be aware of the extra row in the model of course (in such calls as setValueAt(...))

Good luck.

javamonkey79
The only problem with that approach is sorting. I need the footer row to stay at the bottom of the table.
Luke Quinane
+3  A: 

there is an ancient site that shows some examples of what one can do with some of the original Swing components. they provide some very interesting ideas, one of which, the Fixed Row Example on page six of the JTable examples, has something to it that may be of interest. The designer put together 2 tables, one of which is fixed to the bottom of the viewport.

akf
your first link is broken
Cogsy
I fixed the link
ninesided
thanks for that
akf
Excellent site!
Gili
A: 

You could try implementing your own TableCellRenderer that replaces the rendered content of the last visible row with your footer. However this wouldn't be fixed at the bottom of the table, it will likely shift up and down as you scroll.

Cogsy
+4  A: 

Try using a second JTable that uses the same column model as your data table and add your footer data to that table. Add the second (footer) table under your original table.

JTable footer = new JTable(model, table.getColumnModel());
panel.add(BorderLayout.CENTER, table);
panel.add(BorderLayout.SOUTH, footer);
objects
This basically looks like a good solution, but it seems to react very oddly when you try to resize the columns.
Stroboskop
A: 

I guess the best approach (but certainly not the easiest) would be to take a look at the source code for the JTableHeader Component, see how it works and then create your own JTableFooter Component. You can re-use the JTableHeader UI Delegate for the footer, I think the main differences would be in the getHeaderRect() method, where it determines the bounds of a given column header tile.

ninesided
A: 

I am having trouble with this solution: When the main table is in a JScrollPane, the maintable is repainting all the time (probably due to some overlap) and this causes 100% CPU load.

We're currently discussing this in comp.lang.java.gui...

Felix Natter
A: 

Here is another solution mentioned in the java bug database

A solution that works for me is painting a border for the viewport (your JTable must be inside a JScrollPane) ....

Dinesh Bhat