List of usage examples for javax.swing.table TableColumn getHeaderValue
public Object getHeaderValue()
Object
used as the value for the header renderer. From source file:Main.java
public static String headerString(TableColumn tc) { final StringBuilder result = new StringBuilder(); result.append(tc.getHeaderValue()); result.append('[').append(tc.getPreferredWidth()).append(']'); return result.toString(); }
From source file:Main.java
/** * Auto fit the column of a table./*from www. j av a 2s . co m*/ * @param table the table for which to auto fit the columns. * @param columnIndex the index of the column to auto fit. * @param maxWidth the maximum width that a column can take (like Integer.MAX_WIDTH). */ public static void autoFitTableColumn(JTable table, int columnIndex, int maxWidth) { TableModel model = table.getModel(); TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer(); int rowCount = table.getRowCount(); for (int i = columnIndex >= 0 ? columnIndex : model.getColumnCount() - 1; i >= 0; i--) { TableColumn column = table.getColumnModel().getColumn(i); int headerWidth = headerRenderer .getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, 0) .getPreferredSize().width; int cellWidth = 0; for (int j = 0; j < rowCount; j++) { Component comp = table.getDefaultRenderer(model.getColumnClass(i)) .getTableCellRendererComponent(table, table.getValueAt(j, i), false, false, 0, i); int preferredWidth = comp.getPreferredSize().width; // Artificial space to look nicer. preferredWidth += 10; cellWidth = Math.max(cellWidth, preferredWidth); } // Artificial space for the sort icon. headerWidth += 20; column.setPreferredWidth(Math.min(Math.max(headerWidth, cellWidth) + table.getRowMargin(), maxWidth)); if (columnIndex >= 0) { break; } } }
From source file:Main.java
public static void sizeWidthToFitData(JTable table, int vc, int buf) { TableColumn tc = table.getColumnModel().getColumn(vc); int max = table.getTableHeader().getDefaultRenderer() .getTableCellRendererComponent(table, tc.getHeaderValue(), false, false, 0, vc) .getPreferredSize().width;/* www . j av a2s. c o m*/ int vrows = table.getRowCount(); for (int i = 0; i < vrows; i++) { TableCellRenderer r = table.getCellRenderer(i, vc); Object value = table.getValueAt(i, vc); Component c = r.getTableCellRendererComponent(table, value, false, false, i, vc); int w = c.getPreferredSize().width; if (max < w) { max = w; } } tc.setPreferredWidth(max + buf); }
From source file:Main.java
/** * Calculates the optimal width for the header of the given table. The * calculation is based on the preferred width of the header renderer. * * @param table the table to calculate the column width * @param col the column to calculate the widths * @return the width, -1 if error *//* w w w .j a va 2s. c om*/ public static int calcHeaderWidth(JTable table, int col) { if (table == null) return -1; if (col < 0 || col > table.getColumnCount()) { System.out.println("invalid col " + col); return -1; } JTableHeader header = table.getTableHeader(); TableCellRenderer defaultHeaderRenderer = null; if (header != null) defaultHeaderRenderer = header.getDefaultRenderer(); TableColumnModel columns = table.getColumnModel(); TableModel data = table.getModel(); TableColumn column = columns.getColumn(col); int width = -1; TableCellRenderer h = column.getHeaderRenderer(); if (h == null) h = defaultHeaderRenderer; if (h != null) { // Not explicitly impossible Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, col); width = c.getPreferredSize().width + 5; } return width; }
From source file:Main.java
/** * Pack specified column. Sets the preferred width of the specified visible * column so it is will be just wide enough to show the column head and the * widest cell value in the column + {@code margin} each side. * /*from w w w. j a v a2 s . c o m*/ * Taken from http://www.exampledepot.com/egs/javax.swing.table/PackCol.html * * NB: If a table has more than 100 rows, then only the width of the column * header will be checked and not the widest cell value. Otherwise it could * take too long. * * @param table * table with column to pack * @param vColIndex * column to pack * @param margin * margin to leave at each side of the column (resulting in an * additional width of 2*margin pixels). */ public static void 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; // because checking the width of each row can be time consuming, // only do so if less than 101 rows. boolean checkDataWidth = (table.getRowCount() < 101); if (checkDataWidth) { // Get maximum width from all 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); }
From source file:Main.java
/** * Adjusts the widths and heights of the cells of the supplied table to fit their contents. */// ww w . j a v a 2 s .c o m public static void sizeToContents(JTable table) { TableModel model = table.getModel(); TableColumn column = null; Component comp = null; int ccount = table.getColumnModel().getColumnCount(), rcount = model.getRowCount(), cellHeight = 0; for (int cc = 0; cc < ccount; cc++) { int headerWidth = 0, cellWidth = 0; column = table.getColumnModel().getColumn(cc); try { comp = column.getHeaderRenderer().getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); headerWidth = comp.getPreferredSize().width; } catch (NullPointerException e) { // getHeaderRenderer() this doesn't work in 1.3 } for (int rr = 0; rr < rcount; rr++) { Object cellValue = model.getValueAt(rr, cc); comp = table.getDefaultRenderer(model.getColumnClass(cc)).getTableCellRendererComponent(table, cellValue, false, false, 0, cc); Dimension psize = comp.getPreferredSize(); cellWidth = Math.max(psize.width, cellWidth); cellHeight = Math.max(psize.height, cellHeight); } column.setPreferredWidth(Math.max(headerWidth, cellWidth)); } if (cellHeight > 0) { table.setRowHeight(cellHeight); } }
From source file:Main.java
/** * Resizes the columns to match content while keeping the table the same * size. This means that the last column may be larger than the content. *//*from w ww .ja va 2s.co m*/ public static void resizeTableColumns(JTable table) { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); int totalWidth = 0; for (int i = 0; i < table.getColumnCount(); i++) { DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(i); if (i == table.getColumnCount() - 1) { col.setPreferredWidth(table.getWidth() - totalWidth); table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); return; } int width = 0; TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, -1, i); width = Math.max(width, comp.getPreferredSize().width); for (int r = 0; r < table.getRowCount(); r++) { renderer = table.getCellRenderer(r, i); comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i), false, false, r, i); width = Math.max(width, comp.getPreferredSize().width); } totalWidth += width + 2; col.setPreferredWidth(width + 2); } }
From source file:net.sf.webphotos.util.Util.java
/** * PackColumn sets the preferred width of the visible column specified by * vColIndex. The column will be just wide enough to show the column head * and the widest cell in the column. margin pixels are added to the left * and right (resulting in an additional width of 2*margin pixels). * * @param table The table you want to resize a column. * @param vColIndex The column number.//from w w w .ja v a2 s .c o m * @param margin Extra spaces for each side of column. */ public static void packColumn(JTable table, int vColIndex, int margin) { DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(vColIndex); int width; // Get width of column header javax.swing.table.TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } java.awt.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); }
From source file:edu.ku.brc.stats.StatGroupTable.java
/** * Calculates and sets the each column to it preferred size * @param table the table to fix ups/*from www . j ava 2 s. c om*/ */ public static void calcColumnWidths(JTable table) { JTableHeader header = table.getTableHeader(); TableCellRenderer defaultHeaderRenderer = null; if (header != null) { defaultHeaderRenderer = header.getDefaultRenderer(); } TableColumnModel columns = table.getColumnModel(); TableModel data = table.getModel(); int margin = columns.getColumnMargin(); // only JDK1.3 int rowCount = data.getRowCount(); int totalWidth = 0; for (int i = columns.getColumnCount() - 1; i >= 0; --i) { TableColumn column = columns.getColumn(i); int columnIndex = column.getModelIndex(); int width = -1; TableCellRenderer h = column.getHeaderRenderer(); if (h == null) h = defaultHeaderRenderer; if (h != null) // Not explicitly impossible { Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, i); width = c.getPreferredSize().width; } for (int row = rowCount - 1; row >= 0; --row) { TableCellRenderer r = table.getCellRenderer(row, i); Component c = r.getTableCellRendererComponent(table, data.getValueAt(row, columnIndex), false, false, row, i); width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer } if (width >= 0) { column.setPreferredWidth(width + margin); // <1.3: without margin } totalWidth += column.getPreferredWidth(); } // If you like; This does not make sense for two many columns! Dimension size = table.getPreferredScrollableViewportSize(); //if (totalWidth > size.width) { size.height = Math.min(size.height, table.getRowHeight() * visibleRows); size.width = totalWidth; table.setPreferredScrollableViewportSize(size); } }
From source file:MainClass.java
protected int sortedIndexOf(TableColumn tc) { int stop = getColumnCount(); String name = tc.getHeaderValue().toString(); for (int i = 0; i < stop; i++) { if (name.compareTo(getColumn(i).getHeaderValue().toString()) <= 0) { return i; }/*from w w w. j a v a 2 s .co m*/ } return stop; }