public void setColumnWidths() { // See // "http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#custom" int n = getModel().getColumnCount(); for (int j = 0; j < n; ++j) { TableColumn column = getColumnModel().getColumn(j); TableCellRenderer headerRenderer = column.getHeaderRenderer(); if (headerRenderer == null) headerRenderer = getTableHeader().getDefaultRenderer(); // the new 1.3 way Component columnComponent = headerRenderer.getTableCellRendererComponent( this, column.getHeaderValue(), false, false, -1, j); Component cellComponent = getDefaultRenderer(getColumnClass(j)) .getTableCellRendererComponent(this, getModel().getValueAt(0, j), false, false, 0, j); int wantWidth = Math.max( columnComponent.getPreferredSize().width + 10, // fudge factor ... seems to always be too small otherwise (on x86 Linux) cellComponent.getPreferredSize().width + 10 // fudge factor ... seems to always be too small otherwise (on Mac OS X) ); column.setPreferredWidth(wantWidth); } }
@Override public int getRowHeight() { if (myRowHeightIsComputing) { return super.getRowHeight(); } if (myRowHeight < 0) { try { myRowHeightIsComputing = true; for (int row = 0; row < getRowCount(); row++) { for (int column = 0; column < getColumnCount(); column++) { final TableCellRenderer renderer = getCellRenderer(row, column); if (renderer != null) { final Object value = getValueAt(row, column); final Component component = renderer.getTableCellRendererComponent(this, value, true, true, row, column); if (component != null) { final Dimension size = component.getPreferredSize(); myRowHeight = Math.max(size.height, myRowHeight); } } } } } finally { myRowHeightIsComputing = false; } } if (myMinRowHeight == null) { myMinRowHeight = getFontMetrics(UIManager.getFont("Label.font")).getHeight(); } return Math.max(myRowHeight, myMinRowHeight); }
// 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; }
// Implementation of TableCellRenderer interface public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { TableCellRenderer rend = super.getCellRenderer(row, column); Component c = rend.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); c.setBackground(getBack(isSelected, hasFocus, row, column)); c.setForeground(getFore(isSelected, hasFocus, row, column)); return c; }
protected Component getHeaderRenderer(int col) { if ((header == null) || (header.getTable() == null)) { return null; } TableColumn tabCol = header.getColumnModel().getColumn(col); TableCellRenderer renderer = tabCol.getHeaderRenderer(); if (renderer == null) { renderer = header.getDefaultRenderer(); } return renderer.getTableCellRendererComponent( header.getTable(), tabCol.getHeaderValue(), false, false, -1, col); }
/* * Calculated the width based on the column name */ private int getColumnHeaderWidth(int column) { if (!isColumnHeaderIncluded) return 0; TableColumn tableColumn = table.getColumnModel().getColumn(column); Object value = tableColumn.getHeaderValue(); TableCellRenderer renderer = tableColumn.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component c = renderer.getTableCellRendererComponent(table, value, false, false, -1, column); return c.getPreferredSize().width; }
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(); 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); } final Dimension headerSize = headerComponent == null ? new Dimension(0, 0) : headerComponent.getPreferredSize(); final String maxStringValue; final String preferredValue; if (columnInfo.getWidth(this) > 0) { int width = columnInfo.getWidth(this); column.setMaxWidth(width); column.setPreferredWidth(width); column.setMinWidth(width); } else if ((maxStringValue = columnInfo.getMaxStringValue()) != null) { int width = getFontMetrics(getFont()).stringWidth(maxStringValue) + columnInfo.getAdditionalWidth(); width = Math.max(width, headerSize.width); column.setPreferredWidth(width); column.setMaxWidth(width); } else if ((preferredValue = columnInfo.getPreferredStringValue()) != null) { int width = getFontMetrics(getFont()).stringWidth(preferredValue) + columnInfo.getAdditionalWidth(); width = Math.max(width, headerSize.width); column.setPreferredWidth(width); } } }
/** @inheritDoc */ public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component c = candidate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if (c instanceof JLabel && table != null) { ViperTableModel m = getCurrentModel(); int modelIndex = table.convertColumnIndexToModel(column); AttrConfig ac = m.getAttributeForColumn(modelIndex); JLabel l = (JLabel) c; if (ac != null) { int visibility = mediator.getHiders().getAttrConfigVisibility(ac); l.setIcon(outerTablePanel.visibilityIcons[visibility]); } else if (m.getInternalColumn(modelIndex) == ViperTableModel.BY_VALID) { Config config = m.getConfig(); int visibility = mediator.getHiders().getConfigVisibility(m.getConfig()); if (visibility == NodeVisibilityManager.RANGE_LOCKED) { visibility = NodeVisibilityManager.LOCKED; } l.setIcon(outerTablePanel.visibilityIcons[visibility]); } else { l.setIcon(null); } } return c; }
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component component = myDelegate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); component.setEnabled(ElementsChooser.this.isEnabled()); if (component instanceof JComponent) { ((JComponent) component).setBorder(null); } return component; }
public void installUI(JComponent c) { super.installUI(c); if ((header != null) && header.getTable() != null) { originalHeaderRenderer = header.getDefaultRenderer(); if ((originalHeaderRenderer != null) && "sun.swing.table.DefaultTableCellHeaderRenderer" .equals(originalHeaderRenderer.getClass().getName())) { header.setDefaultRenderer(new BaseDefaultHeaderRenderer()); } } }
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component c = tableCellRenderer.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column); if (c instanceof JLabel) { JLabel l = (JLabel) c; l.setHorizontalTextPosition(JLabel.LEFT); int modelColumn = table.convertColumnIndexToModel(column); l.setIcon(getHeaderRendererIcon(modelColumn, l.getFont().getSize())); } return c; }
public void updateColumnSizes() { final JTableHeader header = getTableHeader(); final TableCellRenderer defaultRenderer = header == null ? null : header.getDefaultRenderer(); ColumnInfo[] columns = getListTableModel().getColumnInfos(); for (int i = 0; i < columns.length; i++) { final ColumnInfo columnInfo = columns[i]; final TableColumn column = getColumnModel().getColumn(i); final Component headerComponent = defaultRenderer == null ? null : defaultRenderer.getTableCellRendererComponent( this, column.getHeaderValue(), false, false, 0, 0); final Dimension headerSize = headerComponent == null ? new Dimension(0, 0) : headerComponent.getPreferredSize(); final String maxStringValue; final String preferredValue; if (columnInfo.getWidth(this) > 0) { int width = columnInfo.getWidth(this); column.setMaxWidth(width); column.setPreferredWidth(width); column.setMinWidth(width); } else if ((maxStringValue = columnInfo.getMaxStringValue()) != null) { int width = getFontMetrics(getFont()).stringWidth(maxStringValue) + columnInfo.getAdditionalWidth(); width = Math.max(width, headerSize.width); column.setPreferredWidth(width); column.setMaxWidth(width); } else if ((preferredValue = columnInfo.getPreferredStringValue()) != null) { int width = getFontMetrics(getFont()).stringWidth(preferredValue) + columnInfo.getAdditionalWidth(); width = Math.max(width, headerSize.width); column.setPreferredWidth(width); } } }
/** * Get the dimension of this ColumnGroup. * * @param table the table the header is being rendered in * @return the dimension of the ColumnGroup */ public Dimension getSize(JTable table) { Component comp = renderer.getTableCellRendererComponent(table, getHeaderValue(), false, false, -1, -1); int height = comp.getPreferredSize().height; int width = 0; Iterator iter = v.iterator(); while (iter.hasNext()) { Object obj = iter.next(); if (obj instanceof TableColumn) { TableColumn aColumn = (TableColumn) obj; width += aColumn.getWidth(); } else { width += ((ColumnGroup) obj).getSize(table).width; } } return new Dimension(width, height); }
private VcsLogHighlighter.VcsCommitStyle getStyle( int row, int column, String text, boolean hasFocus, final boolean selected) { Component dummyRendererComponent = myDummyRenderer.getTableCellRendererComponent(this, text, selected, hasFocus, row, column); VisibleGraph<Integer> visibleGraph = getVisibleGraph(); if (row < 0 || row >= visibleGraph.getVisibleCommitCount()) { LOG.error( "Visible graph has " + visibleGraph.getVisibleCommitCount() + " commits, yet we want row " + row); return VcsCommitStyleFactory.createStyle( dummyRendererComponent.getForeground(), dummyRendererComponent.getBackground(), VcsLogHighlighter.TextStyle.NORMAL); } final RowInfo<Integer> rowInfo = visibleGraph.getRowInfo(row); VcsLogHighlighter.VcsCommitStyle defaultStyle = VcsCommitStyleFactory.createStyle( rowInfo.getRowType() == RowType.UNMATCHED ? JBColor.GRAY : dummyRendererComponent.getForeground(), dummyRendererComponent.getBackground(), VcsLogHighlighter.TextStyle.NORMAL); List<VcsLogHighlighter.VcsCommitStyle> styles = ContainerUtil.map( myHighlighters, new Function<VcsLogHighlighter, VcsLogHighlighter.VcsCommitStyle>() { @Override public VcsLogHighlighter.VcsCommitStyle fun(VcsLogHighlighter highlighter) { return highlighter.getStyle(rowInfo.getCommit(), selected); } }); return VcsCommitStyleFactory.combine(ContainerUtil.append(styles, defaultStyle)); }
/** @inheritDoc */ public String toString() { return candidate.toString(); }
/** @inheritDoc */ public int hashCode() { return candidate.hashCode(); }
/** @inheritDoc */ public boolean equals(Object arg0) { return candidate.equals(arg0); }
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); } } }