1 Reply Latest reply: Oct 17, 2012 2:57 AM by Jörg RSS

    Two header table and fixed column

    Jörg
      Hello,

      based on Re: JTable column header to span multiple columns and help from Walter I have written a TwoHeaderTable class, which was satisfying my needs
      so far. But now I have got more columns and I have to move the horizontal scrollBar to see all the table.
      That's why I would like to fix the first (most significant) column, so that it keeps visible all the time when
      scrolling. There are working examples for fixed columns in the net. But to get both features, two headers and a fixed column,
      together, seems to be not so easy. The attempt I tried below gives an error (ArrayIndexOutOfBoundsException)
      right from the beginning. Is there still a chance to get the first column fixed by this approach of removing columns from the model -
      the cause of the Exception?
      import java.awt.*;
      import javax.swing.*;
      import javax.swing.table.*;
      
      public class TwoHeadersAndFixedColumn extends JFrame {
        private Object[][] DATA = {
              {"Banana", 1, 3.1, 10, 7.2},
              {"Peach",  2, 7.3, 100, 8.99},
              {"Apple",  3, 12., 67, 0.5},
              {"Plum",   4, 1.3, 20, 77.7},
              {"Mango",  5, 2., 19, 26.5},
        };
        String[] TOP_HEADER= new String[]{"", "Asia", "Europe"},
              SUB_HEADER= new String[]{"Fruit", "Sample#", "Result",
                                    "Sample#", "Result"};
      
        public TwoHeadersAndFixedColumn() {
          super("TwoHeadersAndFixedColumn");
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setSize(300, 200);
      
          TwoHeaderTable table= new TwoHeaderTable(TOP_HEADER, new int[]{1,2,2});
          DefaultTableModel dtm= createTableModel(SUB_HEADER, true);
          dtm.setDataVector(DATA, SUB_HEADER);
          table.setModel(dtm);
          table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
          TableColumnModel columnModel= table.getColumnModel();
          for (int i=0; i<4; i++)
            columnModel.getColumn(i).setPreferredWidth(80);
      
          JScrollPane scroll= new JScrollPane(table);
          scroll.setColumnHeaderView(table.pHeader);
      
          JTable fixed= new JTable(table.getModel());
          fixed.setFocusable(false);
          fixed.setSelectionModel(table.getSelectionModel());
          fixed.getTableHeader().setResizingAllowed(false);
      
      //  Remove the fixed columns from the main table
          int fixedColumns= 1;
          for (int i=0; i<fixedColumns; i++)
            columnModel.removeColumn(columnModel.getColumn(0));
      
      //  Remove the non-fixed columns from the fixed table
          columnModel= fixed.getColumnModel();
          while (fixed.getColumnCount() > fixedColumns)
            columnModel.removeColumn(columnModel.getColumn(fixedColumns));
          fixed.setPreferredScrollableViewportSize(fixed.getPreferredSize());
          scroll.setRowHeaderView(fixed);
      /*    JViewport viewport = new JViewport();
          viewport.setView(fixed);
          viewport.setPreferredSize(fixed.getPreferredSize());
          scroll.setRowHeaderView(viewport);
          scroll.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixed.getTableHeader());
      */
          add(scroll);
          setVisible(true);
        }
      
      
        public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
            public void run() {
           new TwoHeadersAndFixedColumn();
            }
          });
        }
      
      
        DefaultTableModel createTableModel(String[] columnNames,
                               final boolean editable) {
          DefaultTableModel tblModel= new DefaultTableModel(columnNames, 0) {
            public Class getColumnClass(int column) {
           Class returnValue;
           if (column>=0 && column<getColumnCount()) {
             Object o= getValueAt(0, column);
             returnValue= (o==null) ? Object.class : o.getClass();
           }
           else {
             returnValue= Object.class;
           }
           return returnValue;
            }
      
            // Make read-only
            public boolean isCellEditable(int x, int y) {
           return editable;
            }
          };
          return tblModel;
        }
      
      }
      ////////////////////////////////////////////
      import java.awt.*;
      import java.util.*;
      import javax.swing.*;
      import javax.swing.event.*;
      import javax.swing.table.*;
      
      public class TwoHeaderTable extends JTable {
        int i, j;
        JPanel pHeader;
        TableColumnModel colModel;
        int[] iStartColumn;
        final JTableHeader topHeader;
      
      
      /* @param iSpannedColumns     An array equal to the length of topHeaderNames
                          denoting the count of columns each topHeader
                          spans.
      */
        public TwoHeaderTable(final String[] topHeaderNames,
                     final int[] iSpannedColumns) {
          super();
          colModel= getColumnModel();
          iStartColumn= new int[topHeaderNames.length];
          iStartColumn[0]= 0;
          for (i=1; i<iStartColumn.length; i++)
            iStartColumn= iStartColumn[i-1] + iSpannedColumns[i-1];
      getTableHeader().setReorderingAllowed(false);
      final JTable dummy= new JTable(new Object[][]{}, topHeaderNames);
      topHeader= dummy.getTableHeader();
      topHeader.setReorderingAllowed(false);
      topHeader.setResizingAllowed(false);
      final TableColumnModel dummyModel= dummy.getColumnModel();
      Enumeration<TableColumn> enumDummyColumns= dummyModel.getColumns();
      final ArrayList<TableColumn> dummyColumns=
                               Collections.list(enumDummyColumns);
      colModel.addColumnModelListener(new TableColumnModelListener() {
      @Override
      public void columnAdded(TableColumnModelEvent e) {
      }
      @Override
      public void columnRemoved(TableColumnModelEvent e) {
      }
      @Override
      public void columnMoved(TableColumnModelEvent e) {
      }
      @Override
      public void columnMarginChanged(ChangeEvent e) {
           for (int i=0; i<topHeaderNames.length; i++) { // Local i!
           dummyColumns.get(i).setWidth(
                     addColWidths(iStartColumn[i], iSpannedColumns[i]));
           }
      }
      @Override
      public void columnSelectionChanged(ListSelectionEvent e) {
      }
      });
      pHeader= new JPanel(new GridLayout(2, 1));
      pHeader.add(topHeader);
      pHeader.add(getTableHeader());
      }


      private int addColWidths(int startCol, int iCount) {
      j=0;
      for (i=startCol; i<startCol+iCount; i++)
      j+= colModel.getColumn(i).getWidth();
      return j;
      }


      @Override
      protected void configureEnclosingScrollPane() {
      // Do nothing. Custom table header.
      }

      }
      Edited by: Jörg on 17.10.2012 01:10
      
      Edited by: Jörg on 17.10.2012 01:33                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        • 1. Re: Two header table and fixed column
          Jörg
          Got it: If the fixed column is not defined in the top header, the top header is alright. But for the 1st subheader to have the same height as the rest, it needs to have its own panel as well.
            String[] TOP_HEADER= new String[]{"Asia", "Europe"},
          .
          .
          .
              TwoHeaderTable table= new TwoHeaderTable(TOP_HEADER, new int[]{2,2});
          Adding
          JPanel p1stHeader= new JPanel(new GridLayout(2, 1));
          p1stHeader.add(new JLabel());
          p1stHeader.add(fixed.getTableHeader());
          scroll.setCorner(JScrollPane.UPPER_LEFT_CORNER, p1stHeader());