Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you remove a row from a JTable

Status
Not open for further replies.

dellyjm

Programmer
Apr 13, 2000
168
JM
Hi,

how do you remove a row from a JTable.

Thanks.

Delly.
 
Hi dellyjm:

Check the Java api section for AbstractTableModel.


It has a method called deleteRow(int index);

Any TableModel should implement this method. The DefaultTableModel for example.

Perhaps you should do a repaint to see the changes on the GUI.

Hope it helps. Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
Hi dellyjm:

I was in an error. The DefaultTableModel doesn't implement the deleteRow method. You can code your own TableModel to implement it. Here is an example:

Code:
class TablaModelo extends AbstractTableModel {
    String[] columnNames = {};
    Vector rows = new Vector();
    Vector clases = new Vector();
    
    public TablaModelo(Vector columnNames, Vector rows){
        String [] cn = new String [columnNames.size()];
        for(int i=0;i<columnNames.size();i++){
        	 cn[i]=(String) columnNames.elementAt(i);
        	 clases.add(String.class);
       	}
        	 
        this.columnNames=cn;
        this.rows=rows;
    }

    public void addRow(Vector r) {
    	rows.add(r);
    }
    
    public void deleteRow(int index) {
    	rows.remove(index);
    }
    
    public void deleteAllRows(){
    	rows.removeAllElements();
    }
    //////////////////////////////////////////////////////////////////////////
    //
    //             Implementation of the TableModel Interface
    //
    //////////////////////////////////////////////////////////////////////////



    public String getColumnName(int column) {
        if (columnNames[column] != null) {
            return columnNames[column];
        } else {
            return &quot;&quot;;
        }
    }

    public Class getColumnClass(int column) {
            return (Class) clases.elementAt(column);
    }

    public void setColumnClass(Class clase, int column){
    	clases.setElementAt(clase,column);
    }

    public boolean isCellEditable(int row, int column) {
            return true;
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    // Data methods

    public int getRowCount() {
        return rows.size();
    }

    public Object getValueAt(int aRow, int aColumn) {
        Vector row = (Vector)rows.elementAt(aRow);
        return row.elementAt(aColumn);
    }

    public void setValueAt(Object value, int row, int column) {
        Vector dataRow = (Vector)rows.elementAt(row);
        dataRow.setElementAt(value, column);
        fireTableChanged(new TableModelEvent(this, row, row, column));

    }
}

You can use TablaModelo to create a new table and call the deleteRow method from the model.

Hope it helps. Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top