Java Code Examples for javax.swing.table.TableColumn#getHeaderRenderer()
The following examples show how to use
javax.swing.table.TableColumn#getHeaderRenderer() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TableAligner.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Installs alignment decorators in the table column at {@code colViewIndex}. * * @param table a table. * @param colViewIndex the index of the column in the table <i>view</i>. * @param alignment one of the following constants: * <ul> * <li>{@link SwingConstants#LEFT}</li> * <li>{@link SwingConstants#CENTER} (the default for image-only labels)</li> * <li>{@link SwingConstants#RIGHT}</li> * <li>{@link SwingConstants#LEADING} (the default for text-only labels)</li> * <li>{@link SwingConstants#TRAILING}</li> * </ul> */ public static void installInOneColumn(JTable table, int colViewIndex, int alignment) { TableColumn tableColumn = table.getColumnModel().getColumn(colViewIndex); TableCellRenderer headerRenderer = tableColumn.getHeaderRenderer(); if (headerRenderer == null) { headerRenderer = table.getTableHeader().getDefaultRenderer(); } if (!(headerRenderer instanceof RendererAlignmentDecorator)) { // Don't install a redundant decorator. tableColumn.setHeaderRenderer(new RendererAlignmentDecorator(headerRenderer, alignment)); } TableCellRenderer cellRenderer = tableColumn.getCellRenderer(); if (cellRenderer == null) { cellRenderer = table.getDefaultRenderer(table.getColumnClass(colViewIndex)); } if (!(cellRenderer instanceof RendererAlignmentDecorator)) { // Don't install a redundant decorator. tableColumn.setCellRenderer(new RendererAlignmentDecorator(cellRenderer, alignment)); } }
Example 2
Source File: TaskTextViewerPanel.java From moa with GNU General Public License v3.0 | 6 votes |
private void rescaleTableColumns() { // iterate over all columns to resize them individually TableColumnModel columnModel = previewTable.getColumnModel(); for (int columnIdx = 0; columnIdx < columnModel.getColumnCount(); ++columnIdx) { // get the current column TableColumn column = columnModel.getColumn(columnIdx); // get the renderer for the column header to calculate the preferred // with for the header TableCellRenderer renderer = column.getHeaderRenderer(); // check if the renderer is null if (renderer == null) { // if it is null use the default renderer for header renderer = previewTable.getTableHeader().getDefaultRenderer(); } // create a cell to calculate its preferred size Component comp = renderer.getTableCellRendererComponent(previewTable, column.getHeaderValue(), false, false, 0, columnIdx); int width = comp.getPreferredSize().width; // set the maximum width which was calculated column.setPreferredWidth(width); } }
Example 3
Source File: TableColumnAdjuster.java From magarena with GNU General Public License v3.0 | 6 votes |
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; }
Example 4
Source File: GroupableTableHeaderUI.java From mzmine3 with GNU General Public License v2.0 | 6 votes |
private int getHeaderHeight() { int height = 0; TableColumnModel columnModel = header.getColumnModel(); for (int column = 0; column < columnModel.getColumnCount(); column++) { TableColumn aColumn = columnModel.getColumn(column); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) renderer = header.getDefaultRenderer(); Component comp = renderer.getTableCellRendererComponent(header.getTable(), aColumn.getHeaderValue(), false, false, -1, column); int cHeight = comp.getPreferredSize().height; Enumeration<?> en = ((GroupableTableHeader) header).getColumnGroups(aColumn); if (en != null) { while (en.hasMoreElements()) { ColumnGroup cGroup = (ColumnGroup) en.nextElement(); cHeight += cGroup.getSize(header.getTable()).height; } } height = Math.max(height, cHeight); } return height; }
Example 5
Source File: TableColumnAdjuster.java From keystore-explorer with GNU General Public License v3.0 | 6 votes |
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; }
Example 6
Source File: UIUtilities.java From netbeans with Apache License 2.0 | 6 votes |
/** * Initializes preferred (and eventually maximum) width of a table column * based on the size of its header and the estimated longest value. * * @param table Table to adjust the column width for. * @param index Index of the column. * @param longValue Estimated long value for the column. * @param padding Number of pixes for padding. */ public static void initColumnWidth(JTable table, int index, Object longValue, int padding) { TableColumn column = table.getColumnModel().getColumn(index); // get preferred size of the header TableCellRenderer headerRenderer = column.getHeaderRenderer(); if (headerRenderer == null) { headerRenderer = table.getTableHeader().getDefaultRenderer(); } Component comp = headerRenderer.getTableCellRendererComponent( table, column.getHeaderValue(), false, false, 0, 0); int width = comp.getPreferredSize().width; // get preferred size of the long value (remeber max of the pref. size for header and long value) comp = table.getDefaultRenderer(table.getModel().getColumnClass(index)).getTableCellRendererComponent( table, longValue, false, false, 0, index); width = Math.max(width, comp.getPreferredSize().width) + 2 * padding; // set preferred width of the column column.setPreferredWidth(width); // if the column contains boolean values, the preferred width // should also be its max width if (longValue instanceof Boolean) { column.setMaxWidth(width); } }
Example 7
Source File: GroupableTableHeaderUI.java From mzmine2 with GNU General Public License v2.0 | 6 votes |
private int getHeaderHeight() { int height = 0; TableColumnModel columnModel = header.getColumnModel(); for (int column = 0; column < columnModel.getColumnCount(); column++) { TableColumn aColumn = columnModel.getColumn(column); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) renderer = header.getDefaultRenderer(); Component comp = renderer.getTableCellRendererComponent(header.getTable(), aColumn.getHeaderValue(), false, false, -1, column); int cHeight = comp.getPreferredSize().height; Enumeration<?> en = ((GroupableTableHeader) header).getColumnGroups(aColumn); if (en != null) { while (en.hasMoreElements()) { ColumnGroup cGroup = (ColumnGroup) en.nextElement(); cHeight += cGroup.getSize(header.getTable()).height; } } height = Math.max(height, cHeight); } return height; }
Example 8
Source File: GroupableTableHeaderUI.java From ramus with GNU General Public License v3.0 | 5 votes |
/** * Calculate and return the height of the header. * * @return Header Height */ @SuppressWarnings("unchecked") private int getHeaderHeight() { int height = 0; GroupableTableColumnModel columnModel = (GroupableTableColumnModel) header .getColumnModel(); for (int column = 0; column < columnModel.getColumnCount(); column++) { TableColumn aColumn = columnModel.getColumn(column); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) { renderer = header.getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(header .getTable(), aColumn.getHeaderValue(), false, false, -1, column); int cHeight = comp.getPreferredSize().height; Iterator iter = columnModel.getColumnGroups(aColumn); if (iter != null) { while (iter.hasNext()) { ColumnGroup cGroup = (ColumnGroup) iter.next(); cHeight += cGroup.getSize(header.getTable()).height; } } height = Math.max(height, cHeight); } return height; }
Example 9
Source File: SortingTableModelDecorator.java From jAudioGIT with GNU Lesser General Public License v2.1 | 5 votes |
private int getRealPrefferedHeaderWidth(JTable t, int col) { TableColumn tc = t.getColumn(t.getColumnName(col)); TableCellRenderer tcr = tc.getHeaderRenderer(); if (tcr == null) { tcr = t.getTableHeader().getDefaultRenderer(); } Component c = tcr.getTableCellRendererComponent(t, tc.getHeaderValue(), false, false, 0, 0); return c.getPreferredSize().width; }
Example 10
Source File: EditableHeaderTableColumn.java From chipster with MIT License | 5 votes |
public void copyValues(TableColumn base) { modelIndex = base.getModelIndex(); identifier = base.getIdentifier(); width = base.getWidth(); minWidth = base.getMinWidth(); setPreferredWidth(base.getPreferredWidth()); maxWidth = base.getMaxWidth(); headerRenderer = base.getHeaderRenderer(); headerValue = base.getHeaderValue(); cellRenderer = base.getCellRenderer(); cellEditor = base.getCellEditor(); isResizable = base.getResizable(); }
Example 11
Source File: EditableTableHeaderColumn.java From rapidminer-studio with GNU Affero General Public License v3.0 | 5 votes |
public void copyValues(TableColumn base) { modelIndex = base.getModelIndex(); identifier = base.getIdentifier(); width = base.getWidth(); minWidth = base.getMinWidth(); setPreferredWidth(base.getPreferredWidth()); maxWidth = base.getMaxWidth(); headerRenderer = base.getHeaderRenderer(); headerValue = base.getHeaderValue(); cellRenderer = base.getCellRenderer(); cellEditor = base.getCellEditor(); isResizable = base.getResizable(); }
Example 12
Source File: UIUtil.java From ganttproject with GNU General Public License v3.0 | 5 votes |
public static Dimension getHeaderDimension(JTable table, TableColumn tableColumn) { TableCellRenderer renderer = tableColumn.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(table, tableColumn.getHeaderValue(), false, false, 0, 0); return comp.getPreferredSize(); }
Example 13
Source File: ColumnWidthsResizer.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
public static void resize(JTable table, int colViewIndex, boolean doFullScan) { int maxWidth = 0; // Get header width. TableColumn column = table.getColumnModel().getColumn(colViewIndex); TableCellRenderer headerRenderer = column.getHeaderRenderer(); if (headerRenderer == null) { headerRenderer = table.getTableHeader().getDefaultRenderer(); } Object headerValue = column.getHeaderValue(); Component headerRendererComp = headerRenderer.getTableCellRendererComponent(table, headerValue, false, false, 0, colViewIndex); maxWidth = Math.max(maxWidth, headerRendererComp.getPreferredSize().width); // Get cell widths. if (doFullScan) { for (int row = 0; row < table.getRowCount(); ++row) { maxWidth = Math.max(maxWidth, getCellWidth(table, row, colViewIndex)); } } else { maxWidth = Math.max(maxWidth, getCellWidth(table, 0, colViewIndex)); maxWidth = Math.max(maxWidth, getCellWidth(table, table.getRowCount() / 2, colViewIndex)); maxWidth = Math.max(maxWidth, getCellWidth(table, table.getRowCount() - 1, colViewIndex)); } // For some reason, the calculation above gives a value that is 1 pixel too small. // Maybe that's because of the cell divider line? ++maxWidth; column.setPreferredWidth(maxWidth); }
Example 14
Source File: GroupableTableHeaderUI.java From mzmine2 with GNU General Public License v2.0 | 5 votes |
private void paintCell(Graphics g, Rectangle cellRect, int columnIndex) { TableColumn aColumn = header.getColumnModel().getColumn(columnIndex); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) renderer = header.getDefaultRenderer(); Component component = renderer.getTableCellRendererComponent(header.getTable(), aColumn.getHeaderValue(), false, false, -1, columnIndex); rendererPane.add(component); rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y, cellRect.width, cellRect.height, true); }
Example 15
Source File: SubstanceTableHeaderUI.java From radiance with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void uninstallUI(JComponent c) { for (int i = 0; i < header.getColumnModel().getColumnCount(); i++) { TableColumn aColumn = header.getColumnModel().getColumn(i); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) { renderer = header.getDefaultRenderer(); } Component rendComp = renderer.getTableCellRendererComponent(header.getTable(), aColumn.getHeaderValue(), false, false, -1, i); SwingUtilities.updateComponentTreeUI(rendComp); } super.uninstallUI(c); }
Example 16
Source File: SubstanceTableHeaderUI.java From radiance with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Retrieves renderer for the specified column header. * * @param columnIndex * Column index. * @return Renderer for the specified column header. */ private Component getHeaderRenderer(int columnIndex) { TableColumn aColumn = header.getColumnModel().getColumn(columnIndex); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) { renderer = header.getDefaultRenderer(); } return renderer.getTableCellRendererComponent(header.getTable(), aColumn.getHeaderValue(), false, false, -1, columnIndex); }
Example 17
Source File: DarkTableHeaderUI.java From darklaf with MIT License | 5 votes |
protected Component getHeaderCellRenderer(final int columnIndex) { TableColumn aColumn = header.getColumnModel().getColumn(columnIndex); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) { renderer = header.getDefaultRenderer(); } boolean hasFocus = !header.isPaintingForPrint() && header.hasFocus(); rendererDelegate.setDelegate(renderer); return rendererDelegate.getTableCellRendererComponent(header.getTable(), aColumn.getHeaderValue(), false, hasFocus, -1, columnIndex); }
Example 18
Source File: GUIUtils.java From open-ig with GNU Lesser General Public License v3.0 | 4 votes |
/** * Resizes the table columns based on the column and data preferred widths. * @param table the original table * @param model the data model * @return the table itself */ public static JTable autoResizeColWidth(JTable table, AbstractTableModel model) { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setModel(model); int margin = 5; for (int i = 0; i < table.getColumnCount(); i++) { int vColIndex = i; DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(vColIndex); int width; // 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); } ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment( SwingConstants.LEFT); // table.setAutoCreateRowSorter(true); // table.getTableHeader().setReorderingAllowed(false); // for (int i = 0; i < table.getColumnCount(); i++) { // TableColumn column = table.getColumnModel().getColumn(i); // // column.setCellRenderer(new DefaultTableColour()); // } return table; }
Example 19
Source File: AnimPlayList.java From open-ig with GNU Lesser General Public License v3.0 | 4 votes |
/** * Resizes the table columns based on the column and data preferred widths. * @param table the original table * @param model the data model * @return the table itself */ public static JTable autoResizeColWidth(JTable table, AbstractTableModel model) { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setModel(model); int margin = 5; for (int i = 0; i < table.getColumnCount(); i++) { int vColIndex = i; DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(vColIndex); int width; // 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); } ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment( SwingConstants.LEFT); return table; }
Example 20
Source File: PeakListTableWindow.java From mzmine2 with GNU General Public License v2.0 | 4 votes |
/** * Methods for ActionListener interface implementation */ public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (command.equals("PROPERTIES")) { ExitCode exitCode = parameters.showSetupDialog(this, true); if (exitCode == ExitCode.OK) { int rowHeight = parameters.getParameter(PeakListTableParameters.rowHeight).getValue(); table.setRowHeight(rowHeight); PeakListTableColumnModel cm = (PeakListTableColumnModel) table.getColumnModel(); cm.createColumns(); } } if (command.equals("AUTOCOLUMNWIDTH")) { // Auto size column width based on data for (int column = 0; column < table.getColumnCount(); column++) { TableColumn tableColumn = table.getColumnModel().getColumn(column); if (tableColumn.getHeaderValue() != "Peak shape" && tableColumn.getHeaderValue() != "Status") { TableCellRenderer renderer = tableColumn.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component component = renderer.getTableCellRendererComponent(table, tableColumn.getHeaderValue(), false, false, -1, column); int preferredWidth = component.getPreferredSize().width + 20; tableColumn.setPreferredWidth(preferredWidth); } } } if (command.equals("PRINT")) { try { table.print(PrintMode.FIT_WIDTH); } catch (PrinterException e) { MZmineCore.getDesktop().displayException(this, e); } } }