List of usage examples for javax.swing.table TableColumnModel getColumn
public TableColumn getColumn(int columnIndex);
TableColumn
object for the column at columnIndex
. From source file:Main.java
/** * Maps the index of the column in the view at * {@code viewColumnIndex} to the index of the column * in the table model. Returns the index of the corresponding * column in the model. If {@code viewColumnIndex} * is less than zero, returns {@code viewColumnIndex}. * * @param cm the table model// www .j a va 2 s .c o m * @param viewColumnIndex the index of the column in the view * @return the index of the corresponding column in the model * * @see JTable#convertColumnIndexToModel(int) * @see javax.swing.plaf.basic.BasicTableHeaderUI */ public static int convertColumnIndexToModel(TableColumnModel cm, int viewColumnIndex) { if (viewColumnIndex < 0) { return viewColumnIndex; } return cm.getColumn(viewColumnIndex).getModelIndex(); }
From source file:Main.java
public static int getPreferredTableWidth(final JTable table) { TableColumnModel columnModel = table.getColumnModel(); int width = 0; for (int col = 0; col < columnModel.getColumnCount(); col++) { width += columnModel.getColumn(col).getPreferredWidth(); if (col != 0) { width += columnModel.getColumnMargin(); }/* ww w .j a va 2 s.c om*/ } return width; }
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 *//*from w w w. j a v a 2s . com*/ 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
/** * Maps the index of the column in the {@code cm} at * {@code modelColumnIndex} to the index of the column * in the view. Returns the index of the * corresponding column in the view; returns {@code -1} if this column * is not being displayed. If {@code modelColumnIndex} is less than zero, * returns {@code modelColumnIndex}.//from www.jav a2s . c om * * @param cm the table model * @param modelColumnIndex the index of the column in the model * @return the index of the corresponding column in the view * * @see JTable#convertColumnIndexToView(int) * @see javax.swing.plaf.basic.BasicTableHeaderUI */ public static int convertColumnIndexToView(TableColumnModel cm, int modelColumnIndex) { if (modelColumnIndex < 0) { return modelColumnIndex; } for (int column = 0; column < cm.getColumnCount(); column++) { if (cm.getColumn(column).getModelIndex() == modelColumnIndex) { return column; } } return -1; }
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 va2s . c o 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
/** * 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//from w ww . j av a 2s . c om * @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:net.sf.webphotos.util.Util.java
/** * Ajusta a largura das colunas do modelo. * * @param tabela Tabela que deseja ajustar as colunas. * @param parametros Tamanhos das colunas separadas por vrgula. *///from ww w. j av a2 s .c o m public static void ajustaLargura(JTable tabela, String parametros) { int temR = -1; TableColumnModel modeloColunas = tabela.getColumnModel(); if (parametros == null) { return; } if (parametros.length() > 0) { StringTokenizer tok = new StringTokenizer(parametros, ","); int ct = 0; String l; while (tok.hasMoreTokens()) { l = tok.nextToken(); try { modeloColunas.getColumn(ct).setPreferredWidth(Integer.parseInt(l)); } catch (NumberFormatException nE) { switch (l) { case "*": log.info("Packing column " + ct); packColumn(tabela, ct, 1); break; case "R": temR = ct; break; } } catch (Exception e) { } ct++; } if (temR > 0) { modeloColunas.getColumn(temR).setPreferredWidth(modeloColunas.getColumn(temR).getPreferredWidth() + tabela.getWidth() - modeloColunas.getTotalColumnWidth()); log.debug("Tamanho da tabela: " + (modeloColunas.getColumn(temR).getPreferredWidth() + tabela.getWidth() - modeloColunas.getTotalColumnWidth())); } //Testes log.debug("Tamanho Total: " + modeloColunas.getTotalColumnWidth()); log.debug("Tamanho da tabela: " + tabela.getWidth()); } }
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 w w w. j a va 2s .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:GenderRenderer.java
public TableCellRendererJComboBox() { Container pane = getContentPane(); pane.setLayout(new BorderLayout()); TableValues tv = new TableValues(); table = new JTable(tv); TableColumnModel tcm = table.getColumnModel(); TableColumn tc = tcm.getColumn(TableValues.GENDER); tc.setCellRenderer(new GenderRenderer()); JScrollPane jsp = new JScrollPane(table); pane.add(jsp, BorderLayout.CENTER); }
From source file:ru.codemine.pos.ui.windows.selector.ProductSelector.java
@Override public void selectFor(GenericDocumentWindow window) { this.clientWindow = window; if (!actionListenersInit) setupActionListeners();/*w w w . ja va 2s. c om*/ List<Product> products = productService.getAll(); tableModel = new ProductCatalogTableModel(products); table.setModel(tableModel); TableColumnModel columnModel = table.getColumnModel(); columnModel.getColumn(0).setMaxWidth(10); columnModel.getColumn(1).setMaxWidth(10); setTitle(" ? ?"); statusLabel.setText(" " + products.size() + " ?"); setupSorter(); setVisible(true); }