// Initialize JTable private void initTable() { DialogTableModel tblModel = new DialogTableModel(students); studentsTbl.setModel(tblModel); studentsTbl.setAutoCreateRowSorter(true); studentsTbl.setRowSelectionAllowed(true); studentsTbl.getRowSorter().toggleSortOrder(1); studentsTbl.setGridColor(Color.gray); studentsTbl.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); studentsTbl.setRowHeight(22); TableColumn tc = studentsTbl.getColumnModel().getColumn(0); tc.setCellEditor(studentsTbl.getDefaultEditor(Boolean.class)); tc.setCellRenderer(studentsTbl.getDefaultRenderer(Boolean.class)); tc.setHeaderRenderer( new CheckBoxTableHeader( new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { Object source = e.getSource(); if (source instanceof AbstractButton == false) { return; } boolean checked = e.getStateChange() == ItemEvent.SELECTED; for (int x = 0, y = studentsTbl.getRowCount(); x < y; x++) { studentsTbl.setValueAt(new Boolean(checked), x, 0); } } })); }
private static void setRenderers(JTable table, String type) { final MetricTableModel model = (MetricTableModel) table.getModel(); final MetricInstance[] metrics = model.getMetricsInstances(); Arrays.sort(metrics, new MetricInstanceAbbreviationComparator()); final TableColumnModel columnModel = table.getColumnModel(); for (int i = 0; i < model.getColumnCount(); i++) { final String columnName = model.getColumnName(i); final TableColumn column = columnModel.getColumn(i); if (columnName.equals(type)) { column.setCellRenderer(new MetricCellRenderer(null)); column.setHeaderRenderer(new HeaderRenderer(null, model, SwingConstants.LEFT)); } else { final MetricInstance metricInstance = model.getMetricForColumn(i); final TableCellRenderer renderer = new MetricCellRenderer(metricInstance); column.setCellRenderer(renderer); final Metric metric = metricInstance.getMetric(); final String displayName = metric.getDisplayName(); column.setHeaderRenderer(new HeaderRenderer(displayName, model, SwingConstants.RIGHT)); } } }