public void resizeColumnWidth(JTable table) { final TableColumnModel columnModel = table.getColumnModel(); for (int column = 0; column < table.getColumnCount(); column++) { int width = 15; // Min width for (int row = 0; row < table.getRowCount(); row++) { TableCellRenderer renderer = table.getCellRenderer(row, column); Component comp = table.prepareRenderer(renderer, row, column); width = Math.max(comp.getPreferredSize().width +1 , width); } if(width > 300) width=300; columnModel.getColumn(column).setPreferredWidth(width); } }
private void copyMatchListSelectionsToClipboard() { int[] selectionIndices = resultsTable.getSelectedRows(); int colCount = resultsTable.getColumnCount(); StringBuilder sb = new StringBuilder(); for (int r = 0; r < selectionIndices.length; r++) { if (r > 0) { sb.append('\n'); } sb.append(resultsTable.getValueAt(selectionIndices[r], 0)); for (int c = 1; c < colCount; c++) { sb.append('\t'); sb.append(resultsTable.getValueAt(selectionIndices[r], c)); } } StringSelection ss = new StringSelection(sb.toString()); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); }
@Override public void componentResized(ComponentEvent componentEvent) { super.componentResized(componentEvent); Dimension tableSize = table.getSize(); final int[] columnSizes = new int[]{ 20, 20, 20, 40 }; for (int i = 0; i < table.getColumnCount(); i++) { TableColumn column = table.getColumnModel().getColumn(i); column.setPreferredWidth((int) (tableSize.width * (columnSizes[i] / 100f))); } } });
JTable table = new JTable( ... ); table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF ); for (int column = 0; column < table.getColumnCount(); column++) { TableColumn tableColumn = table.getColumnModel().getColumn(column); int preferredWidth = tableColumn.getMinWidth(); int maxWidth = tableColumn.getMaxWidth(); for (int row = 0; row < table.getRowCount(); row++) { TableCellRenderer cellRenderer = table.getCellRenderer(row, column); Component c = table.prepareRenderer(cellRenderer, row, column); int width = c.getPreferredSize().width + table.getIntercellSpacing().width; preferredWidth = Math.max(preferredWidth, width); // We've exceeded the maximum width, no need to check other rows if (preferredWidth >= maxWidth) { preferredWidth = maxWidth; break; } } tableColumn.setPreferredWidth( preferredWidth ); }
public static void setColumnWidthAndOrder(JTable table, int[] defaultColumnsWidth, String widthPrefKey, String orderPrefKey) { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // set the column width from saved value or defaults int[] widths = getIntArrayFromString(PreferencesDialog.getCachedValue(widthPrefKey, null)); int i = 0; for (int width : defaultColumnsWidth) { if (widths != null && widths.length > i) { width = widths[i]; } if (table.getColumnModel().getColumnCount() >= i) { TableColumn column = table.getColumnModel().getColumn(i++); column.setWidth(width); column.setPreferredWidth(width); } else { break; } } // set the column order int[] order = getIntArrayFromString(PreferencesDialog.getCachedValue(orderPrefKey, null)); if (order != null && order.length == table.getColumnCount()) { for (int j = 0; j < table.getColumnCount(); j++) { table.moveColumn(table.convertColumnIndexToView(order[j]), j); } } }
for (int row : getSelectedRows()) { Rectangle start = getCellRect(row, 0, true); Rectangle end = getCellRect(row, getColumnCount() - 1, true); g2.setPaint(new GradientPaint(start.x, 0, controlColor, (int) ((end.x + end.width - start.x) * 1.25), 0, Color.orange)); g2.fillRect(start.x, start.y, end.x + end.width - start.x, start.height);
for (int i = 0; i < mainTable.getColumnCount(); i++) { adjustColumnSizes(mainTable, i, 2); for (int row = 0; row < jTable.getRowCount(); row++) { int maxHeight = 0; for (int column = 0; column < jTable.getColumnCount(); column++) { TableCellRenderer cellRenderer = jTable.getCellRenderer(row, column); Object valueAt = jTable.getValueAt(row, column);
for (int row : getSelectedRows()) { Rectangle start = getCellRect(row, 0, true); Rectangle end = getCellRect(row, getColumnCount() - 1, true); g2.setPaint(new GradientPaint(start.x, 0, controlColor, (int) ((end.x + end.width - start.x) * 1.25), 0, Color.orange)); g2.fillRect(start.x, start.y, end.x + end.width - start.x, start.height);
for (int i = 0; i < mainTable.getColumnCount(); i++) { adjustColumnSizes(mainTable, i, 2);
if (table.getColumnCount() > 0) { TableColumn colToDelete = table.getColumnModel().getColumn(table.getColumnCount() - 1); table.removeColumn(colToDelete); table.validate();
if (table.getColumnCount() > 0) { TableColumn colToDelete = table.getColumnModel().getColumn(table.getColumnCount() - 1); table.removeColumn(colToDelete); table.validate();
for (int i = 0 ; i < this.tableValues.getColumnCount() ; i++) { rowSorter.setComparator(i, comparatorNumeric);
public static void clearTable(final JTable table) { for (int i = 0; i < table.getRowCount(); i++) for(int j = 0; j < table.getColumnCount(); j++) { table.setValueAt("", i, j); } } }
private int getColumnByName(JTable table, String name) { for (int i = 0; i < table.getColumnCount(); ++i) if (table.getColumnName(i).equals(name)) return i; return -1; }
/** * Resizes all columns to fit all their content. */ public void resizeAllColumns() { for (int col = 0; col < table.getColumnCount(); col++) { TableColumn column = table.getColumnModel().getColumn(col); adjustColumnWidth(column); } }
public static void autoSizeTableColumns(JTable table) { for (int column = 0; column < table.getColumnCount(); column++) { int columnWidth = 0; for (int row = 0; row < table.getRowCount(); row++) { TableCellRenderer renderer = table.getCellRenderer(row, column); Component comp = table.prepareRenderer(renderer, row, column); columnWidth = Math.max(comp.getPreferredSize().width, columnWidth); } table.getColumnModel().getColumn(column).setPreferredWidth(columnWidth); } }
JTable table = new JTable(my_rows, my_header); for (int c = 0; c < table.getColumnCount(); c++) { Class<?> col_class = table.getColumnClass(c); table.setDefaultEditor(col_class, null); // remove editor }
protected void updateRowHeights() { for (int row = 0; row < this.mainList.getRowCount(); row++) { int rowHeight = this.mainList.getRowHeight(); for (int column = 0; column < this.mainList.getColumnCount(); column++) { Component comp = this.mainList.prepareRenderer(this.mainList.getCellRenderer(row, column), row, column); rowHeight = Math.max(rowHeight, comp.getPreferredSize().height); } this.mainList.setRowHeight(row, rowHeight); } }
JTable table = new JTable(rows, cols); table.setAutoCreateColumnsFromModel(false); DefaultTableModel model = (DefaultTableModel)table.getModel(); TableColumn col = new TableColumn(model.getColumnCount()); col.setHeaderValue(headerLabel); table.addColumn(col); model.addColumn(headerLabel.toString(), values); table.moveColumn(table.getColumnCount()-1, vColIndex);
public void windowClosing(java.awt.event.WindowEvent evt) { int numCols = tracerTable.getColumnCount(); for (int i = 0; i < numCols; i++) uiMgr.putInt("studio/tracer_col_width/" + i, tracerTable.getColumnModel().getColumn(i).getWidth()); uiMgr.putInt("studio/tracer_win_pos_x", tracerFrame.getX()); uiMgr.putInt("studio/tracer_win_pos_y", tracerFrame.getY()); uiMgr.putInt("studio/tracer_win_pos_w", tracerFrame.getWidth()); uiMgr.putInt("studio/tracer_win_pos_h", tracerFrame.getHeight()); tracerFrame.dispose(); } });