List of usage examples for javax.swing.table TableColumn getCellRenderer
public TableCellRenderer getCellRenderer()
TableCellRenderer
used by the JTable
to draw values for this column. From source file:au.com.jwatmuff.eventmanager.util.GUIUtils.java
public static void leftAlignTable(JTable table) { Enumeration<TableColumn> cols = table.getColumnModel().getColumns(); while (cols.hasMoreElements()) { TableColumn col = cols.nextElement(); final TableCellRenderer oldRenderer = col.getCellRenderer(); if (oldRenderer == null) { col.setCellRenderer(new DefaultTableCellRenderer() { @Override//ww w. j ava2 s .c om public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if (c instanceof JLabel) { ((JLabel) c).setHorizontalAlignment(SwingConstants.LEFT); } return c; } }); } else { col.setCellRenderer(new TableCellRenderer() { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component c = oldRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if (c instanceof JLabel) { ((JLabel) c).setHorizontalAlignment(SwingConstants.LEFT); } return c; } }); } } }
From source file:edu.ku.brc.specify.tasks.subpane.ESResultsTablePanel.java
/** * @param table/*from ww w .j av a 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); }
From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java
protected Column addRuntimeGeneratedColumn(String columnId) { // store old cell editors / renderers TableCellEditor[] cellEditors = new TableCellEditor[tableModel.getColumnCount() + 1]; TableCellRenderer[] cellRenderers = new TableCellRenderer[tableModel.getColumnCount() + 1]; for (int i = 0; i < tableModel.getColumnCount(); i++) { Column tableModelColumn = tableModel.getColumn(i); if (tableModel.isGeneratedColumn(tableModelColumn)) { TableColumn tableColumn = getColumn(tableModelColumn); cellEditors[i] = tableColumn.getCellEditor(); cellRenderers[i] = tableColumn.getCellRenderer(); }// ww w .j a v a2s . co m } // if column with columnId does not exists then add new to model Column col = new Column(columnId, columnId); col.setEditable(false); columns.put(col.getId(), col); // do not touch columnsOrder, it will be synced from table model if (tableModel != null) { tableModel.addColumn(col); } // reassign column identifiers setColumnIdentifiers(); // reattach old generated columns for (int i = 0; i < tableModel.getColumnCount(); i++) { Column tableModelColumn = tableModel.getColumn(i); if (tableModel.isGeneratedColumn(tableModelColumn)) { TableColumn tableColumn = getColumn(tableModelColumn); if (cellEditors[i] != null) { tableColumn.setCellEditor(cellEditors[i]); } if (cellRenderers[i] != null) { tableColumn.setCellRenderer(cellRenderers[i]); } } } return col; }
From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java
@Override public void removeColumn(Column column) { if (column == null) { return;/*w w w . j av a2 s .c om*/ } String name; if (column.getId() instanceof MetaPropertyPath) { MetaPropertyPath metaPropertyPath = (MetaPropertyPath) column.getId(); name = metaPropertyPath.getMetaProperty().getName(); editableColumns.remove(metaPropertyPath); } else { name = column.getId().toString(); } TableColumn tableColumn = null; Iterator<TableColumn> columnIterator = getAllColumns().iterator(); while (columnIterator.hasNext() && (tableColumn == null)) { TableColumn xColumn = columnIterator.next(); Object identifier = xColumn.getIdentifier(); if (identifier instanceof String && identifier.equals(name)) { tableColumn = xColumn; } else if (column.equals(identifier)) { tableColumn = xColumn; } } if (tableColumn != null) { // store old cell editors / renderers Map<Object, TableCellEditor> cellEditors = new HashMap<>(); Map<Object, TableCellRenderer> cellRenderers = new HashMap<>(); for (int i = 0; i < tableModel.getColumnCount(); i++) { Column tableModelColumn = tableModel.getColumn(i); if (tableModel.isGeneratedColumn(tableModelColumn)) { TableColumn oldColumn = getColumn(tableModelColumn); cellEditors.put(tableModelColumn.getId(), oldColumn.getCellEditor()); cellRenderers.put(tableModelColumn.getId(), oldColumn.getCellRenderer()); } } impl.getColumnModel().removeColumn(tableColumn); impl.removeColumn(tableColumn); columns.remove(column.getId()); columnsOrder.remove(column); if (tableModel != null) { tableModel.removeColumn(column); } // reassign column identifiers setColumnIdentifiers(); // reattach old generated columns for (int i = 0; i < tableModel.getColumnCount(); i++) { Column tableModelColumn = tableModel.getColumn(i); if (tableModel.isGeneratedColumn(tableModelColumn)) { TableColumn oldColumn = getColumn(tableModelColumn); if (cellEditors.containsKey(tableModelColumn.getId())) { oldColumn.setCellEditor(cellEditors.get(tableModelColumn.getId())); } if (cellRenderers.containsKey(tableModelColumn.getId())) { oldColumn.setCellRenderer(cellRenderers.get(tableModelColumn.getId())); } } } packRows(); repaintImplIfNeeded(); } column.setOwner(null); }
From source file:org.pentaho.reporting.designer.core.util.table.ElementMetaDataTable.java
public TableCellRenderer getCellRenderer(final int row, final int viewColumn) { final int column = convertColumnIndexToModel(viewColumn); final Object value = getModel().getValueAt(row, column); if (value instanceof GroupingHeader) { return groupingCellRenderer; }//from ww w . j a va 2s . co m final ElementMetaDataTableModel model = (ElementMetaDataTableModel) getModel(); final Class columnClass = model.getClassForCell(row, column); if (columnClass.isArray()) { return arrayCellRenderer; } final PropertyEditor propertyEditor = model.getEditorForCell(row, column); if (propertyEditor != null) { propertyEditorCellRenderer.setPropertyEditor(propertyEditor); return propertyEditorCellRenderer; } final TableColumn tableColumn = getColumnModel().getColumn(column); final TableCellRenderer renderer = tableColumn.getCellRenderer(); if (renderer != null) { return renderer; } final TableCellRenderer defaultRenderer = getDefaultRenderer(columnClass); if (defaultRenderer != null) { return defaultRenderer; } if (logger.isTraceEnabled()) { logger.trace("No renderer for column class " + columnClass); // NON-NLS } return getDefaultRenderer(Object.class); }
From source file:org.pentaho.reporting.libraries.designtime.swing.table.PropertyTable.java
public TableCellRenderer getCellRenderer(final int row, final int viewColumn) { final TableModel tableModel = getModel(); if (tableModel instanceof PropertyTableModel) { final PropertyTableModel model = (PropertyTableModel) getModel(); final int column = convertColumnIndexToModel(viewColumn); final Class columnClass = model.getClassForCell(row, column); if (columnClass.isArray()) { return arrayCellRenderer; }//from ww w .j av a 2 s . c o m final PropertyEditor propertyEditor = model.getEditorForCell(row, column); if (propertyEditor != null) { propertyEditorCellRenderer.setPropertyEditor(propertyEditor); return propertyEditorCellRenderer; } final TableColumn tableColumn = getColumnModel().getColumn(column); final TableCellRenderer renderer = tableColumn.getCellRenderer(); if (renderer != null) { return renderer; } final TableCellRenderer defaultRenderer = getDefaultRenderer(columnClass); if (defaultRenderer != null) { return defaultRenderer; } if (logger.isTraceEnabled()) { logger.trace("No renderer for column class " + columnClass); // NON-NLS } return getDefaultRenderer(Object.class); } return super.getCellRenderer(row, viewColumn); }
From source file:org.yccheok.jstock.gui.JTableUtilities.java
/** * Sets the editor/renderer for Date objects to provided JTable, for the specified column. * @param table JTable to set up//from w w w .j a v a 2 s .c o m * @param row Column to apply */ public static void setDateEditorAndRendererForRow(JTable table, int row) { final TableColumn column = table.getColumnModel().getColumn(row); // SwingX's. Pretty but buggy. //column.setCellEditor(new DatePickerCellEditor()); column.setCellEditor(new DateFieldTableEditor()); final DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT); column.setCellRenderer(new DateRendererDecoratorEx(column.getCellRenderer(), format)); }