JTable footer string

What is the best way to put a footer string in a JTable? Does anyone have some sample code for this?

The only approach I was thinking about is to put a special row in the table model, which is always sorted from below.


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 great, but choosing a footer is a bit weird.

+4
source share
8 answers

Try using a second JTable, which uses the same column model as your data table, and adds footer data to this table. Add a second table (footer) under your original table.

 JTable footer = new JTable(model, table.getColumnModel()); panel.add(BorderLayout.CENTER, table); panel.add(BorderLayout.SOUTH, footer); 
+5
source

It looks like the project has a JideScrollPane component that advertises support for row footer. I have not tried this myself, but it looks like it does exactly what you want! The website also has a demo application in which you can see it in action, and it looks very good.

Please note that you think they need a lot, but their JideScrollPane looks free and open.

+2
source

Using two tables below each other is a good approach.

If you want to be able to resize / move / delete columns, the key should NOT reuse the same column between tables. Ask the listener to resize. Example:

 package snippet; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; import javax.swing.SwingUtilities; import javax.swing.event.ChangeEvent; import javax.swing.event.ListSelectionEvent; import javax.swing.event.TableColumnModelEvent; import javax.swing.event.TableColumnModelListener; import javax.swing.table.TableColumnModel; public class FixedRow2Tables extends JFrame { private static final long serialVersionUID = 4676303089799270571L; Object[][] data; Object[] column; JTable footerTable, table; public FixedRow2Tables() { super("Fixed Row Example"); Object[][] mainData = new Object[][] { { "a", "", "", "", "", "" }, { "", "b", "", "", "", "" }, { "", "", "c", "", "", "" }, { "", "", "", "d", "", "" }, { "", "", "", "", "e", "" }, { "", "", "", "", "", "f" } }; Object[][] summaryData = { { "fixed1", "", "", "", "", "" }, { "fixed2", "", "", "", "", "" } }; column = new Object[] { "A", "B", "C", "D", "E", "F" }; table = new JTable(mainData, column); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); footerTable = new JTable(summaryData, column); footerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); footerTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); footerTable.setTableHeader(null); // footerTable.setColumnModel(table.getColumnModel()); table.getColumnModel().addColumnModelListener( new TableColumnModelListener() { @Override public void columnSelectionChanged(ListSelectionEvent e) { } @Override public void columnRemoved(TableColumnModelEvent e) { } @Override public void columnMoved(TableColumnModelEvent e) { } @Override public void columnMarginChanged(ChangeEvent e) { final TableColumnModel tableColumnModel = table .getColumnModel(); TableColumnModel footerColumnModel = footerTable .getColumnModel(); for (int i = 0; i < tableColumnModel.getColumnCount(); i++) { int w = tableColumnModel.getColumn(i).getWidth(); footerColumnModel.getColumn(i).setMinWidth(w); footerColumnModel.getColumn(i).setMaxWidth(w); // footerColumnModel.getColumn(i).setPreferredWidth(w); } footerTable.doLayout(); footerTable.repaint(); repaint(); } @Override public void columnAdded(TableColumnModelEvent e) { } }); JScrollPane scroll = new JScrollPane(table); scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scroll.setPreferredSize(new Dimension(400, 100)); getContentPane().add(scroll, BorderLayout.CENTER); getContentPane().add(footerTable, BorderLayout.SOUTH); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { FixedRow2Tables frame = new FixedRow2Tables(); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.pack(); frame.setVisible(true); } }); } } 
+1
source

The only time I did this, I simply added a line to the model as follows:

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

_tableContents is, of course, the actual data behind my model. Of course, you will need to know the extra line in the model (in calls like setValueAt (...))

Good luck.

0
source

You can try to implement your own TableCellRenderer , which replaces the displayed content of the last visible row with your footer. However, this will not be fixed at the bottom of the table; it will most likely move up and down as you scroll.

0
source

I think the best approach (but certainly not the easiest) is to look at the source code for the JTableHeader Component, see how it works, and then create your own JTableFooter Component. You can reuse the JTableHeader delegate interface for the footer, I think the main differences will be in the getHeaderRect() method, where it defines the bounds of the given header of the header column.

0
source

Here is another solution mentioned in java bug database

The solution that works for me draws a border for the viewport (your JTable should be inside JScrollPane) ....

0
source
 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){} } 
0
source

Source: https://habr.com/ru/post/1286044/


All Articles