List of usage examples for javax.swing.table TableCellRenderer getTableCellRendererComponent
Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column);
From source file:org.jdal.swing.table.AnnotationFormatTableCellRenderer.java
/** * {@inheritDoc}// ww w.j a va 2 s . co m */ @SuppressWarnings("unchecked") @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Class<?> clazz = table.getColumnClass(column); if (Number.class.isAssignableFrom(clazz)) { TableModel tableModel = table.getModel(); if (tableModel instanceof ListTableModel) { ListTableModel listTableModel = (ListTableModel) tableModel; Class<?> modelClass = ((ListTableModel) tableModel).getModelClass(); String propertyName = listTableModel.getPropertyName(column); Printer<Object> printer = (Printer<Object>) FormatUtils.getPrinter(modelClass, propertyName); if (printer != null) { value = printer.print(value, Locale.getDefault()); JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); label.setHorizontalAlignment(JLabel.RIGHT); return label; } } } TableCellRenderer renderer = table.getDefaultRenderer(clazz); return renderer != null ? renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column) : null; }
From source file:Main.java
public Main() { JTable table = new JTable(3, 3); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); for (int i = 0; i < table.getColumnCount(); i++) { DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(i); int width = 0; TableCellRenderer renderer = col.getHeaderRenderer(); for (int r = 0; r < table.getRowCount(); r++) { renderer = table.getCellRenderer(r, i); Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i), false, false, r, i);/* w w w . j a va 2 s. c om*/ width = Math.max(width, comp.getPreferredSize().width); } col.setPreferredWidth(width + 2); } JFrame f = new JFrame(); f.add(new JScrollPane(table)); f.setSize(300, 300); f.setVisible(true); }
From source file:Main.java
public Main() { JTable table = new JTable(3, 3); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); for (int i = 0; i < table.getColumnCount(); i++) { DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(i); int width = 0; TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); }//from w ww . j av a2 s . c o m Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0); width = comp.getPreferredSize().width; col.setPreferredWidth(width + 2); } JFrame f = new JFrame(); f.add(new JScrollPane(table)); f.setSize(300, 300); f.setVisible(true); }
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// www . j a v a 2 s . com 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:TableRenderDemo.java
private void initColumnSizes(JTable table) { MyTableModel model = (MyTableModel) table.getModel(); TableColumn column = null;//w w w . ja va2 s .com Component comp = null; int headerWidth = 0; int cellWidth = 0; Object[] longValues = model.longValues; TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer(); for (int i = 0; i < 5; i++) { column = table.getColumnModel().getColumn(i); comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); headerWidth = comp.getPreferredSize().width; comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, longValues[i], false, false, 0, i); cellWidth = comp.getPreferredSize().width; if (DEBUG) { System.out.println("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth + "; cellWidth = " + cellWidth); } // XXX: Before Swing 1.1 Beta 2, use setMinWidth instead. column.setPreferredWidth(Math.max(headerWidth, cellWidth)); } }
From source file:components.TableRenderDemo.java
private void initColumnSizes(JTable table) { MyTableModel model = (MyTableModel) table.getModel(); TableColumn column = null;/*from ww w.j av a 2s. com*/ Component comp = null; int headerWidth = 0; int cellWidth = 0; Object[] longValues = model.longValues; TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer(); for (int i = 0; i < 5; i++) { column = table.getColumnModel().getColumn(i); comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); headerWidth = comp.getPreferredSize().width; comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, longValues[i], false, false, 0, i); cellWidth = comp.getPreferredSize().width; if (DEBUG) { System.out.println("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth + "; cellWidth = " + cellWidth); } column.setPreferredWidth(Math.max(headerWidth, cellWidth)); } }
From source file:GeMSE.GS.Analysis.Stats.OneSampleCovariancePanel.java
public void ResizeColumnWidth(JTable table) { final TableColumnModel columnModel = table.getColumnModel(); for (int column = 0; column < table.getColumnCount(); column++) { int width = 50; // Min width TableColumn tableColumn = columnModel.getColumn(column); TableCellRenderer renderer = tableColumn.getHeaderRenderer(); if (renderer == null) renderer = table.getTableHeader().getDefaultRenderer(); Component component = renderer.getTableCellRendererComponent(table, tableColumn.getHeaderValue(), false, false, -1, column);/*from www . j a v a 2 s . c om*/ width = Math.max(component.getPreferredSize().width + 5, width); for (int row = 0; row < table.getRowCount(); row++) { renderer = table.getCellRenderer(row, column); Component comp = table.prepareRenderer(renderer, row, column); width = Math.max(comp.getPreferredSize().width + 5, width); } if (width > 400) width = 400; columnModel.getColumn(column).setPreferredWidth(width); } }
From source file:edu.ku.brc.specify.plugins.sgr.SGRResultsDisplay.java
/** * From http://www.pikopong.com/blog/2008/08/13/auto-resize-jtable-column-width/ * /*from w w w.j a va 2 s . co m*/ * @param table * @param model * @return */ private JTable autoResizeColWidth(JTable table, DefaultTableModel 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 = 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); } ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()) .setHorizontalAlignment(SwingConstants.LEFT); // table.setAutoCreateRowSorter(true); //table.getTableHeader().setReorderingAllowed(false); return table; }
From source file:logdruid.ui.table.StatRecordingEditorTable.java
private void initColumnSizes(JTable theTable) { MyTableModel model = (MyTableModel) theTable.getModel(); TableColumn column = null;// w w w.j av a2 s . co m Component comp = null; int headerWidth = 0; int cellWidth = 0; // Object[] longValues = model.longValues; TableCellRenderer headerRenderer = theTable.getTableHeader().getDefaultRenderer(); for (int i = 0; i < 6; i++) { column = theTable.getColumnModel().getColumn(i); comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); headerWidth = comp.getPreferredSize().width; cellWidth = comp.getPreferredSize().width; if (DEBUG) { logger.info("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth + "; cellWidth = " + cellWidth); } column.setPreferredWidth(Math.max(headerWidth, cellWidth)); } }
From source file:logdruid.ui.mainpanel.EventRecordingSelectorPanel.java
private void initColumnSizes(JTable theTable) { TableColumn column = null;/*from ww w . j a va 2 s . co m*/ Component comp = null; int headerWidth = 0; int cellWidth = 0; TableCellRenderer headerRenderer = theTable.getTableHeader().getDefaultRenderer(); for (int i = 0; i < 4; i++) { column = theTable.getColumnModel().getColumn(i); comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); headerWidth = comp.getPreferredSize().width; cellWidth = comp.getPreferredSize().width; column.setPreferredWidth(Math.max(headerWidth, cellWidth)); } }