1

I would need to create a JTable (Java Swing) with a footer row (that will contain the sum of the data for each column).

The specific needs are:

  • Footer is always visible (like the header): the parent JScrollPane should only scroll between the header and the footer (i.e. the actual data)
  • Table columns are not resizeable (which should make things easier)
  • Depending on screen resolution, table can be scrolled horizontally and/or vertically

Reading similar questions, it seems that the best approach is to add a second table (with only 1 row) below the main table, which is what I did:

        JPanel result = new JPanel(new BorderLayout());
        SessionTable sessionTbl = null; //This is a JTable
        AbstractSessionTableModel sessionTableModel = null;
        JScrollPane sessionScrollPane = null;

(...)

        //Inits scrollpane
        sessionScrollPane = new JScrollPane(sessionTbl);
        sessionScrollPane.setPreferredSize(new Dimension(100, 50));
        sessionScrollPane.setBorder(BorderFactory.createEmptyBorder());

        //Adds tables
        result.add(sessionScrollPane, BorderLayout.CENTER);

        //Adds footer table (which is also contained in a JScrollPane and has the same columns than the main table)
        result.add(createSessionFooterTable(sessionTableModel), BorderLayout.SOUTH);

Unfortunately this approach has 2 problems (see picture):

  • Horizontal scroll only moves the main table (i.e. the footer is not moving since it is in a different JScrollPane)
  • The horizontal scroll bar appears between the 2 tables

Screenshot

I then tried an alternative approach: Add the 2 tables in a same JPanel before adding them to a common JScrollPane. Unfortunately this approach also has 2 problems:

  • The header of the main table is not visible anymore
  • The footer row is part of the scrolling (i.e. it is not always visible)

Any help or hint would be greatly appreciated !

Many thanks! Thomas

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Tom
  • 1,375
  • 3
  • 24
  • 45

1 Answers1

2
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thanks for suggesting this but unfortunately the piece of code has 2 issues: (1) It does not have a horizontal scroll (cells width are adjusted to available space while in my case the cell widths are fixed) (2) When the vertical scroll appears, the footers are not aligned anymore with the table Any other idea / suggestion? Thx – Tom Apr 02 '13 at 18:27
  • synchronize two JScrollBars together, working code about AdjustmentListener are there more than 3-4times, [maybe in my post here too](http://stackoverflow.com/search?q=user%3A714968+JScrollBars+AdjustmentListener) :-) – mKorbel Apr 02 '13 at 19:05