Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Footer row in a JTable

Tags:

java

swing

jtable

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.

like image 548
Luke Quinane Avatar asked Jun 11 '09 01:06

Luke Quinane


2 Answers

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);
like image 158
objects Avatar answered Oct 05 '22 17:10

objects


Looks like this project has a component called JideScrollPane which advertises support for a row footer. I haven't tried it myself, but it sounds like it does exactly what you want! The website also has a demo app where you can see it in action and it that looks pretty good.

Note that it seems a lot of the their stuff you have to pay for, but their JideScrollPane looks to be free and open source.

like image 41
Umi Avatar answered Oct 05 '22 16:10

Umi