Java Utililty Methods JTable Column

List of utility methods to do JTable Column

Description

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

Method

voidaceptarTeclaPresionada(KeyEvent e, JTable pTabla, JTextField pTextoDeBusqueda, int pColumna)
aceptar Tecla Presionada
if (pTabla.getRowCount() <= 0)
    return;
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
    if (pTabla.getRowCount() - 1 > pTabla.getSelectedRow()) {
        showCell(pTabla, pTabla.getSelectedRow() + 1, 0);
        if (pTextoDeBusqueda != null)
            setearTextoDeBusqueda(pTabla, pTextoDeBusqueda, pColumna);
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
    if (pTabla.getSelectedRow() > 0) {
        showCell(pTabla, pTabla.getSelectedRow() - 1, 0);
        if (pTextoDeBusqueda != null)
            setearTextoDeBusqueda(pTabla, pTextoDeBusqueda, pColumna);
} else if (e.getKeyCode() == KeyEvent.VK_PAGE_DOWN) {
    int vNum = (int) java.lang.Math
            .round(pTabla.getVisibleRect().getHeight() / (pTabla.getRowHeight() + pTabla.getRowMargin()));
    if ((pTabla.getRowCount() > pTabla.getSelectedRow())
            && ((pTabla.getSelectedRow() + vNum) <= pTabla.getRowCount()))
        showCell(pTabla, pTabla.getSelectedRow() + vNum, 0);
    else
        showCell(pTabla, pTabla.getRowCount() - 1, 0);
    if (pTextoDeBusqueda != null)
        setearTextoDeBusqueda(pTabla, pTextoDeBusqueda, pColumna);
} else if (e.getKeyCode() == KeyEvent.VK_PAGE_UP) {
    int vNum = (int) java.lang.Math
            .round(pTabla.getVisibleRect().getHeight() / (pTabla.getRowHeight() + pTabla.getRowMargin()));
    if ((pTabla.getSelectedRow() > 0) && ((pTabla.getSelectedRow() - vNum) >= 0))
        showCell(pTabla, pTabla.getSelectedRow() - vNum, 0);
    else
        showCell(pTabla, 0, 0);
    if (pTextoDeBusqueda != null)
        setearTextoDeBusqueda(pTabla, pTextoDeBusqueda, pColumna);
voidaddSpaceSelection(final JTable table, final int column)
If this table has a editable boolean column, the user can press space to toggle the selection of the selected rows for the column.
table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
table.addKeyListener(new KeyAdapter() {
    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            TableModel model = table.getModel();
            for (int row : table.getSelectedRows()) {
                int rowModel = table.convertRowIndexToModel(row);
...
voidajustarAnchoColumnas(JTable table)
ajustar Ancho Columnas
TableColumnModel columnModel = table.getColumnModel();
for (int col = 0; col < table.getColumnCount(); col++) {
    int maxwidth = 0;
    for (int row = 0; row < table.getRowCount(); row++) {
        TableCellRenderer rend = table.getCellRenderer(row, col);
        Object value = table.getValueAt(row, col);
        Component comp = rend.getTableCellRendererComponent(table, value, false, false, row, col);
        maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
...
booleanbuscarDescripcion(KeyEvent e, JTable pTabla, JTextField pTextoDeBusqueda, int pColumna)
buscar Descripcion
boolean findRecord = false;
if ((e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) && (e.getKeyCode() != KeyEvent.VK_ENTER)) {
    String vFindText = pTextoDeBusqueda.getText().toUpperCase();
    String vDescrip = "";
    for (int k = 0; k < pTabla.getRowCount(); k++) {
        vDescrip = ((String) pTabla.getValueAt(k, pColumna)).toUpperCase().trim();
        if (vDescrip.length() >= vFindText.length()) {
            vDescrip = vDescrip.substring(0, vFindText.length());
...
TableColumncloneTableColumn(TableColumn c)
clone Table Column
TableColumn result = new TableColumn(c.getModelIndex(), c.getPreferredWidth(), c.getCellRenderer(),
        c.getCellEditor());
result.setMaxWidth(c.getMaxWidth());
result.setMinWidth(c.getMinWidth());
result.setResizable(c.getResizable());
result.setHeaderValue(c.getHeaderValue());
result.setHeaderRenderer(c.getHeaderRenderer());
result.setIdentifier(c.getIdentifier());
...
TableColumnModelcloneTableColumnModel(TableColumnModel m)
clone Table Column Model
DefaultTableColumnModel result = new DefaultTableColumnModel();
for (int i = 0; i < m.getColumnCount(); i++)
    result.addColumn(cloneTableColumn(m.getColumn(i)));
return result;
CcolumnAt(final JTable table, final Point p, final Class columnClazz)
column At
return columnClazz.getEnumConstants()[table.columnAtPoint(p)];
intconvertColumnIndexToModel(TableColumnModel cm, int viewColumnIndex)
Maps the index of the column in the view at viewColumnIndex to the index of the column in the table model.
if (viewColumnIndex < 0) {
    return viewColumnIndex;
return cm.getColumn(viewColumnIndex).getModelIndex();
intconvertColumnIndexToView(TableColumnModel model, int modelCol)
convert Column Index To View
if (modelCol < 0)
    return modelCol;
for (int viewCol = 0; viewCol < model.getColumnCount(); viewCol++) {
    if (model.getColumn(viewCol).getModelIndex() == modelCol) {
        return viewCol;
return -1;
...
int[]convertSelectedColumnsToModelIndices(JTable table, int[] columns)
convert Selected Columns To Model Indices
int[] convertedCols = new int[columns.length];
for (int i = 0; i < columns.length; i++) {
    convertedCols[i] = table.convertColumnIndexToModel(columns[i]);
return convertedCols;