List of usage examples for javax.swing.table DefaultTableModel getColumnClass
public Class<?> getColumnClass(int columnIndex)
Object.class
regardless of columnIndex
. From source file:edu.ku.brc.specify.plugins.sgr.SGRResultsDisplay.java
private JTable createTable(DefaultTableModel resultsTableModel, final List<List<Color>> rowColors) { JTable table = new JTable(resultsTableModel) { public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int colIndex) { Component c = super.prepareRenderer(renderer, rowIndex, colIndex); final Color bgColor = isCellSelected(rowIndex, colIndex) ? getSelectionBackground() : rowColors.get(rowIndex).get(colIndex); c.setBackground(bgColor);/*w ww . j ava2 s . c o m*/ return c; } }; DefaultTableCellRenderer tcr = getTableCellRenderer();//model.getRowInfoList()); for (int i = 0; i < resultsTableModel.getColumnCount(); i++) { if (resultsTableModel.getColumnClass(i) != Boolean.class) { table.setDefaultRenderer(resultsTableModel.getColumnClass(i), tcr); } } table.setCellSelectionEnabled(true); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); UIHelper.makeTableHeadersCentered(table, false); table.setDefaultRenderer(String.class, new BiColorTableCellRenderer(false)); table.setRowHeight(ROW_HEIGHT); autoResizeColWidth(table, resultsTableModel); return table; }
From source file:edu.ku.brc.specify.tasks.subpane.ESResultsTablePanel.java
/** * @param table//w ww .j a va 2 s. c o m * @param model */ protected void autoResizeColWidth(final JTable table, final DefaultTableModel model) { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setModel(model); int margin = 5; DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); int preferredWidthTotal = 0; int renderedWidthTotal = 0; int[] colWidths = new int[table.getColumnCount()]; int[] strWidths = new int[table.getColumnCount()]; for (int i = 0; i < table.getColumnCount(); i++) { int vColIndex = i; TableColumn col = colModel.getColumn(vColIndex); int width = 0; TableCellRenderer headerRenderer = col.getHeaderRenderer(); if (headerRenderer instanceof JLabel) { ((JLabel) headerRenderer).setHorizontalAlignment(SwingConstants.CENTER); } // Get width of column header TableCellRenderer renderer = col.getCellRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0); width = comp.getPreferredSize().width; // Get maximum width of column data int strWidth = 0; boolean isString = model.getColumnClass(i) == String.class; for (int r = 0; r < table.getRowCount(); r++) { renderer = table.getCellRenderer(r, vColIndex); Object objVal = table.getValueAt(r, vColIndex); if (isString && objVal != null) { strWidth = Math.max(strWidth, ((String) objVal).length()); } comp = renderer.getTableCellRendererComponent(table, objVal, false, false, r, vColIndex); width = Math.max(width, comp.getPreferredSize().width); } // Add margin width += 2 * margin; preferredWidthTotal += col.getPreferredWidth(); colWidths[i] = width; strWidths[i] = strWidth; renderedWidthTotal += width; } if (renderedWidthTotal > preferredWidthTotal) { for (int i = 0; i < table.getColumnCount(); i++) { TableColumn col = colModel.getColumn(i); TableCellRenderer renderer = col.getCellRenderer(); if (renderer != null) { ((JLabel) renderer).setHorizontalAlignment( strWidths[i] > 20 ? SwingConstants.LEFT : SwingConstants.CENTER); //((JLabel)renderer).setHorizontalAlignment(SwingConstants.LEFT); } if (model.getColumnCount() > 3 && renderedWidthTotal > preferredWidthTotal) { col.setPreferredWidth(colWidths[i]); } } } ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()) .setHorizontalAlignment(SwingConstants.LEFT); // table.setAutoCreateRowSorter(true); table.getTableHeader().setReorderingAllowed(false); }