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 ro
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
class Application extends JFrame
{
public Application()
{
this.setBounds(100,100,500,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String data[][] = {{"a1","b1","c1"},{"a2","b2","c2"},{"a3","b3","c3"}};
String columnNames[] = {"a","b","c"};
JTable jtable = new JTable(new DefaultTableModel(data,columnNames));
JScrollPane jscrollPane = new JScrollPane(jtable,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jscrollPane.setBorder(new CompoundBorder(new MatteBorder(0,0,1,0,Color.gray),new EmptyBorder(0,0,0,0)));
this.add(jscrollPane,BorderLayout.CENTER);
JTable jtable_footer = new JTable(new DefaultTableModel(3,columnNames.length),jtable.getColumnModel());
SyncListener syncListener = new SyncListener(jtable,jtable_footer);
this.add(jtable_footer,BorderLayout.SOUTH);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Application application = new Application();
application.setVisible(true);
}
});
}
}
class SyncListener implements TableColumnModelListener
{
JTable jtable_data;
JTable jtable_footer;
public SyncListener(JTable main, JTable footer)
{
jtable_data = main;
jtable_footer = footer;
DefaultTableColumnModel dtcm = (DefaultTableColumnModel)jtable_data.getColumnModel();
dtcm.removeColumnModelListener(dtcm.getColumnModelListeners()[1]);
dtcm.addColumnModelListener(this);
}
public void columnMarginChanged(ChangeEvent changeEvent)
{
for (int column = 0; column < jtable_data.getColumnCount(); column++)
{
jtable_footer.getColumnModel().getColumn(column).setWidth(jtable_data.getColumnModel().getColumn(column).getWidth());
}
jtable_footer.repaint();
}
public void columnAdded(TableColumnModelEvent e){}
public void columnMoved(TableColumnModelEvent e){}
public void columnRemoved(TableColumnModelEvent e){}
public void columnSelectionChanged(ListSelectionEvent e){}
}