jtable

Limpiar JTable JAVA: Los componentes JTable los cuales habíamos tenido la oportunidad de estudiarlos en entradas anteriores, nos proporcionan facilitad al momento de listar información, ya sea en gran volumen o pequeños listados se maneja muy bien, al momento de usarlos caemos en la necesidad de «vaciarlos» o «limpiarlos» en un momento determinado, bien sea por que deseamos llenar el Jtable con nueva información o por que tenemos instanciado un formulario y deseamos iniciarlo vacío, hay múltiples eventualidades que pueden presentarse, en esta entrada veremos como limpiar un Jtable de 2 formas 1 con un ciclo for y otro asignando el modelo por defecto, así que según sus necesidades escoja la opción que mas se ajuste veamos:

Limpiar JTable JAVA Netbeans

Mediante un ciclo for

private void Clear_Table1(){
for (int i = 0; i < tabla1.getRowCount(); i++) {
modelo1.removeRow(i);
i-=1;
}
}

Asignando el modelo por defecto

tabla.setModel(new DefaultTableModel());

7 comentario en “Limpiar Jtable JAVA Netbeans”
  1. y solo hacemos esto:

    modeloTabla.setColumnCount(0);
    modeloTabla.setRowCount(0);

    despues de investigar mucho, incluso intentar tu consejo, mejor lei la documentacion

    setColumnCount
    public void setColumnCount(int columnCount)
    Sets the number of columns in the model. If the new size is greater than the current size, new columns are added to the end of the model with null cell values. If the new size is less than the current size, all columns at index columnCount and greater are discarded.
    Parameters:
    columnCount – the new number of columns in the model
    Since:
    1.3
    See Also:
    setColumnCount(int)
    __________________________________________________________________________________
    setRowCount
    public void setRowCount(int rowCount)
    Sets the number of rows in the model. If the new size is greater than the current size, new rows are added to the end of the model If the new size is less than the current size, all rows at index rowCount and greater are discarded.
    Since:
    1.3
    See Also:
    setColumnCount(int)

  2. DefaultTableModel diseño = new DefaultTableModel();//NO ES NECESARIO PONERLO
    //ES PARA QUE VEAN DE DONDE SALE diseño

    //Para limpiar toda las filas de la tabla menos las columnas

    while (diseño.getRowCount() > 0)
    {
    diseño.removeRow(0);
    }
    //Fin de limpiar las filas

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

×