List of usage examples for javax.swing JTable getColumnModel
public TableColumnModel getColumnModel()
From source file:Main.java
public static void removeTableColumn(JTable table, String columnIdentifier) { try {//from w w w .j a va2 s .c o m TableColumn column = table.getColumn(columnIdentifier); table.getColumnModel().removeColumn(column); } catch (Exception ex) { } }
From source file:Main.java
/** * Adjusts the widths and heights of the cells of the supplied table to fit their contents. *///from ww w . j av a2s .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
/** * 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. * /*www.jav a2s . 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
/** * Save the contents of a table to a TSV file * Note: uses toString() on the header cells as well as the data cells. If you've got funny columns, * expect funny behavior/* w ww . j ava2 s .c o m*/ * @param table * @param outFile * @throws IOException */ public static void SaveTableAsTSV(JTable table, File outFile) throws IOException { PrintWriter outPW = new PrintWriter(outFile); TableModel tableModel = table.getModel(); TableColumnModel columnModel = table.getColumnModel(); StringBuffer headerLineBuf = new StringBuffer(); for (int i = 0; i < columnModel.getColumnCount(); i++) { if (i > 0) headerLineBuf.append("\t"); headerLineBuf.append(columnModel.getColumn(i).getHeaderValue().toString()); } outPW.println(headerLineBuf.toString()); outPW.flush(); for (int i = 0; i < tableModel.getRowCount(); i++) { StringBuffer lineBuf = new StringBuffer(); for (int j = 0; j < tableModel.getColumnCount(); j++) { if (j > 0) lineBuf.append("\t"); lineBuf.append(tableModel.getValueAt(i, j).toString()); } outPW.println(lineBuf.toString()); outPW.flush(); } outPW.close(); }
From source file:Main.java
/** * Calculates the optimal width for the column of the given table. The * calculation is based on the preferred width of the header and cell * renderer.//from ww w .j a v a2 s. co m * <br> * Taken from the newsgoup de.comp.lang.java with some modifications.<br> * Taken from FOPPS/EnhancedTable - http://fopps.sourceforge.net/<br> * * @param table the table to calculate the column width * @param col the column to calculate the widths * @return the width, -1 if error */ public static int calcColumnWidth(JTable table, int col) { int width = calcHeaderWidth(table, col); if (width == -1) return width; TableColumnModel columns = table.getColumnModel(); TableModel data = table.getModel(); int rowCount = data.getRowCount(); TableColumn column = columns.getColumn(col); try { for (int row = rowCount - 1; row >= 0; --row) { Component c = table.prepareRenderer(table.getCellRenderer(row, col), row, col); width = Math.max(width, c.getPreferredSize().width + 10); } } catch (Exception e) { e.printStackTrace(); } return width; }
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 w w . j av a2s . c o 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: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//from w w w .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:Main.java
@SuppressWarnings("serial") public static void setupTable(JTable jTable, TableCellRenderer tableCellRenderer, String[][] content) { jTable.setModel(new DefaultTableModel(content, content[0]) { @Override/*from ww w . j ava 2s .c o m*/ public boolean isCellEditable(int i, int j) { return false; } }); for (int j = 0; j < jTable.getColumnModel().getColumnCount(); j++) { jTable.getColumnModel().getColumn(j).setCellRenderer(tableCellRenderer); } jTable.doLayout(); }
From source file:Main.java
/** * Auto fit the column of a table.//w ww. j a va 2 s .c o 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:org.drugis.mtc.gui.results.ResultsComponentFactory.java
private static JTable createTableWithoutHeaders(NetworkRelativeEffectTableModel dm) { final JTable table = new JTable(dm); table.setTableHeader(null);/* w ww . j a v a2 s. c o m*/ table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); for (final TableColumn c : Collections.list(table.getColumnModel().getColumns())) { c.setMinWidth(170); c.setPreferredWidth(170); } TableCopyHandler.registerCopyAction(table); return table; }