// This method from http://www.exampledepot.com/egs/javax.swing.table/PackCol.html public int packColumn(JTable table, int vColIndex, int margin) { DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(vColIndex); int width = 0; // Get width of column header TableCellRenderer renderer = col.getHeaderRenderer(); 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 for (int r = 0; r < table.getRowCount(); r++) { renderer = table.getCellRenderer(r, vColIndex); comp = renderer.getTableCellRendererComponent( table, table.getValueAt(r, vColIndex), false, false, r, vColIndex); width = Math.max(width, comp.getPreferredSize().width); } // Add margin width += 2 * margin; // Set the width col.setPreferredWidth(width); return width; }
/** * Constructs a <code>VariabilityRecordTable</code> with a list of variability records. * * @param record_list the list of variability records. * @param desktop the parent desktop. */ public VariabilityRecordTable(Vector record_list, net.aerith.misao.gui.Desktop desktop) { this.record_list = record_list; this.desktop = desktop; index = new ArrayIndex(record_list.size()); model = new DefaultTableModel(column_names, 0); Object[] objects = new Object[column_names.length]; objects[0] = new Boolean(true); for (int i = 1; i < column_names.length; i++) objects[i] = ""; for (int i = 0; i < record_list.size(); i++) model.addRow(objects); setModel(model); column_model = (DefaultTableColumnModel) getColumnModel(); for (int i = 1; i < column_names.length; i++) column_model .getColumn(i) .setCellRenderer( new StringRenderer(column_names[i], LabelTableCellRenderer.MODE_MULTIPLE_SELECTION)); initializeCheckColumn(); setTableHeader(new TableHeader(column_model)); setAutoResizeMode(JTable.AUTO_RESIZE_OFF); initializeColumnWidth(); pane = this; initPopupMenu(); }
/** Initializes the column width. */ protected void initializeColumnWidth() { column_model.getColumn(0).setPreferredWidth(20); column_model.getColumn(1).setPreferredWidth(100); column_model.getColumn(2).setPreferredWidth(100); column_model.getColumn(3).setPreferredWidth(100); column_model.getColumn(4).setPreferredWidth(60); column_model.getColumn(5).setPreferredWidth(60); column_model.getColumn(6).setPreferredWidth(60); column_model.getColumn(7).setPreferredWidth(40); column_model.getColumn(8).setPreferredWidth(40); column_model.getColumn(9).setPreferredWidth(40); column_model.getColumn(10).setPreferredWidth(160); column_model.getColumn(11).setPreferredWidth(160); }