Java Utililty Methods JTable Cell

List of utility methods to do JTable Cell

Description

The list of methods to do JTable Cell are organized into topic(s).

Method

intgetWidestCellInColumn(JTable table, TableColumn col)
get Widest Cell In Column
int column = col.getModelIndex();
int max = 0;
for (int row = 0; row < table.getRowCount(); ++row) {
    TableCellRenderer renderer = table.getCellRenderer(row, column);
    Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(row, column), false,
            false, row, column);
    max = Math.max(comp.getPreferredSize().width, max);
return max;
intgetWidestCellInColumn(TableColumn col, JTable table)
Figures out the width of the widest cell in a TableColumn Lifted from Graphic Java's chapter on Tables.
String methodName = MODULE_NAME + "getWidestCellInColumn(TableColumn, JTable)";
int retval = -1;
int modelIndex = col.getModelIndex();
int width = 0;
int maxWidth = 0;
for (int i = 0; i < table.getRowCount(); i++) {
    TableCellRenderer renderer = table.getCellRenderer(i, modelIndex);
    Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(i, modelIndex), false,
...
booleanisCellVisible(JTable table, int rowIndex, int vColIndex)
is Cell Visible
if (!(table.getParent() instanceof JViewport)) {
    return false;
JViewport viewport = (JViewport) table.getParent();
Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
Point pt = viewport.getViewPosition();
rect.setLocation(rect.x - pt.x, rect.y - pt.y);
return new Rectangle(viewport.getExtentSize()).contains(rect);
...
voidrenderHTMLCell(final StringBuffer buffer, final String contents, final String css, final int align)
Render a single table cell in HTML with an optional style.
String strAlign;
strAlign = ALIGN_LEFT;
if (align == SwingConstants.LEFT) {
    strAlign = ALIGN_LEFT;
} else if (align == SwingConstants.RIGHT) {
    strAlign = ALIGN_RIGHT;
} else if (align == SwingConstants.CENTER) {
    strAlign = ALIGN_CENTER;
...
voidsetCellTextAllignment(JTable table, int alignment)
set Cell Text Allignment
DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(alignment);
TableModel tableModel = table.getModel();
for (int columnIndex = 0; columnIndex < tableModel.getColumnCount(); columnIndex++) {
    table.getColumnModel().getColumn(columnIndex).setCellRenderer(rightRenderer);
voidshowCell(JTable table, int row, int column)
Show cell.
Rectangle rect = table.getCellRect(row, column, true);
table.scrollRectToVisible(rect);
table.clearSelection();
table.setRowSelectionInterval(row, row);
voidshowCell(JTable table, int row, int column)
show Cell
if (row >= table.getRowCount())
    row = table.getRowCount() - 1;
Rectangle rect = table.getCellRect(row, column, true);
table.scrollRectToVisible(rect);
table.clearSelection();
table.setRowSelectionInterval(row, row);
voidstartEditingAtCell(JTable table, int row, int column)
start Editing At Cell
table.editCellAt(row, column);
table.getEditorComponent().requestFocusInWindow();