/** * Calculates statistical values for a data array. * * @param data the data array * @return the max, min, mean, SD, SE and non-NaN data count */ private double[] getStatistics(double[] data) { double max = -Double.MAX_VALUE; double min = Double.MAX_VALUE; double sum = 0.0; double squareSum = 0.0; int count = 0; for (int i = 0; i < data.length; i++) { if (Double.isNaN(data[i])) { continue; } count++; max = Math.max(max, data[i]); min = Math.min(min, data[i]); sum += data[i]; squareSum += data[i] * data[i]; } double mean = sum / count; double sd = count < 2 ? Double.NaN : Math.sqrt((squareSum - count * mean * mean) / (count - 1)); if (max == -Double.MAX_VALUE) max = Double.NaN; if (min == Double.MAX_VALUE) min = Double.NaN; return new double[] {max, min, mean, sd, sd / Math.sqrt(count), count}; }
public void updateColumnSizes() { final JTableHeader header = getTableHeader(); final TableCellRenderer defaultRenderer = header == null ? null : header.getDefaultRenderer(); final RowSorter<? extends TableModel> sorter = getRowSorter(); final List<? extends RowSorter.SortKey> current = sorter == null ? null : sorter.getSortKeys(); ColumnInfo[] columns = getListTableModel().getColumnInfos(); int[] sizeMode = new int[columns.length]; int[] headers = new int[columns.length]; int[] widths = new int[columns.length]; int allColumnWidth = 0; int varCount = 0; // calculate for (int i = 0; i < columns.length; i++) { final ColumnInfo columnInfo = columns[i]; final TableColumn column = getColumnModel().getColumn(i); // hack to get sort arrow included into the renderer component if (sorter != null && columnInfo.isSortable()) { sorter.setSortKeys( Collections.singletonList(new RowSorter.SortKey(i, SortOrder.ASCENDING))); } final Component headerComponent = defaultRenderer == null ? null : defaultRenderer.getTableCellRendererComponent( this, column.getHeaderValue(), false, false, 0, 0); if (sorter != null && columnInfo.isSortable()) { sorter.setSortKeys(current); } if (headerComponent != null) { headers[i] = headerComponent.getPreferredSize().width; } final String maxStringValue; final String preferredValue; if (columnInfo.getWidth(this) > 0) { sizeMode[i] = 1; int width = columnInfo.getWidth(this); widths[i] = width; } else if ((maxStringValue = columnInfo.getMaxStringValue()) != null) { sizeMode[i] = 2; widths[i] = getFontMetrics(getFont()).stringWidth(maxStringValue) + columnInfo.getAdditionalWidth(); varCount++; } else if ((preferredValue = columnInfo.getPreferredStringValue()) != null) { sizeMode[i] = 3; widths[i] = getFontMetrics(getFont()).stringWidth(preferredValue) + columnInfo.getAdditionalWidth(); varCount++; } allColumnWidth += widths[i]; } // apply: distribute available space between resizable columns // and make sure that header will fit as well int viewWidth = getParent() != null ? getParent().getWidth() : getWidth(); double gold = 0.5 * (3 - Math.sqrt(5)); int addendum = varCount == 0 || viewWidth < allColumnWidth ? 0 : (int) ((allColumnWidth < gold * viewWidth ? gold * viewWidth : allColumnWidth < (1 - gold) * viewWidth ? (1 - gold) * viewWidth : viewWidth) - allColumnWidth) / varCount; for (int i = 0; i < columns.length; i++) { TableColumn column = getColumnModel().getColumn(i); int width = widths[i]; if (sizeMode[i] == 1) { column.setMaxWidth(width); column.setPreferredWidth(width); column.setMinWidth(width); } else if (sizeMode[i] == 2) { width = Math.max(width + addendum, headers[i]); column.setPreferredWidth(width); column.setMaxWidth(width); } else if (sizeMode[i] == 3) { width = Math.max(width + addendum, headers[i]); column.setPreferredWidth(width); } } }