1
votes

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

1

1 Answers