ColumnFactory { public void packColumn(JXTable table, TableColumnExt columnExt, int margin, int max) { if (!columnExt.isVisible()) throw new IllegalStateException("column must be visible to pack"); int column = table.convertColumnIndexToView(columnExt.getModelIndex()); int width = 0; TableCellRenderer headerRenderer = getHeaderRenderer(table, columnExt); if (headerRenderer != null) { Component comp = headerRenderer.getTableCellRendererComponent(table, columnExt.getHeaderValue(), false, false, 0, column); width = comp.getPreferredSize().width; } TableCellRenderer renderer = getCellRenderer(table, columnExt); for (int r = 0; r < getRowCount(table); r++) { Component comp = table.prepareRenderer(renderer, r, column); width = Math.max(width, comp.getPreferredSize().width); } if (margin < 0) { margin = getDefaultPackMargin(); } width += 2 * margin; if (max != -1 && width > max) width = max; columnExt.setPreferredWidth(width); }  static synchronized ColumnFactory getInstance(); static synchronized void setInstance(ColumnFactory factory); TableColumnExt createAndConfigureTableColumn(TableModel model, int modelIndex); TableColumnExt createTableColumn(int modelIndex); void configureTableColumn(TableModel model, TableColumnExt columnExt); void configureColumnWidths(JXTable table, TableColumnExt columnExt); int getPreferredScrollableViewportWidth(JXTable table); void packColumn(JXTable table, TableColumnExt columnExt, int margin,
            int max); int getDefaultPackMargin(); void setDefaultPackMargin(int margin);  }
@Test public void testPackColumnWithMax() { JXTable table = new JXTable(new AncientSwingTeam()); TableColumnExt columnExt = table.getColumnExt(0); table.getColumnFactory().packColumn(table, columnExt, -1, -1); int prefWidth = columnExt.getPreferredWidth(); assertTrue("sanity: ", prefWidth > 10); int max = prefWidth - 5; table.getColumnFactory().packColumn(table, columnExt, -1, max); assertEquals("pref width must be bounded by max", max, columnExt.getPreferredWidth()); }
@Test public void testPackHiddenColumn() { JXTable table = new JXTable(10, 4); TableColumnExt columnExt = table.getColumnExt(0); columnExt.setVisible(false); try { table.getColumnFactory().packColumn(table, columnExt, -1, -1); fail("packColumn is doc'ed to not handle hidden columns"); } catch (IllegalStateException e) { } }
@Test public void testEncapsulateRowCount() { final int cutOffRowCount = 10; ColumnFactory factory = new ColumnFactory() { @Override protected int getRowCount(JXTable table) { return cutOffRowCount; } }; DefaultTableModel model = new DefaultTableModel(cutOffRowCount * 2, 2) { @Override public Object getValueAt(int row, int column) { if (row >= cutOffRowCount) { throw new IllegalArgumentException("Illegal access to cutoff rows"); } return super.getValueAt(row, column); } }; JXTable table = new JXTable(); table.setColumnFactory(factory); table.setModel(model); factory.packColumn(table, table.getColumnExt(0), -1, -1); }