🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #12654

How to delete a row in a JTable

#12654
From: Phillipus
Date: Tue, 17 Nov 1998 00:00
75 lines
2459 bytes
Does anyone know how to properly delete a row in a JTable?

I have a JTable whose model is an implementation of AbstractTableModel.
The objects that are to be displayed in the table are stored in a Vector
so that we have something like this:

public class MyTableModel extends AbstractTableModel implements
Cloneable, Serializable {
    private Vector myObjectList;
    private JTable table;  // the table whose model this is

    public MessageTableModel(JTable table) {
        super();
        this.table = table;
        myObjectList = new Vector();
    }

    public void addObject(Object myObject) {
        myObjectList.addElement(myObject);
        fireTableRowsInserted(getRowCount(), getRowCount());
    }

    public void removeObject(Object myObject) {
        myObjectList.removeElement(myObject);

        // NOW WHAT?????????
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Object myObject = (Object)myObjectList.elementAt(rowIndex);
        return myObject;
    }

    public int getColumnCount() {
        return 2;
    }

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

    public String getColumnName(int columnIndex) {
        String retVal = "";
        switch(columnIndex) {
            case 0:
                retVal =  "Subject";
                break;
            case 1:
                retVal =  "Date";
                break;
        }
        return retVal;
    }

}


addObject works and the table is updated.  But when I remove an object
from the Vector the program calls getValueAt and obviously the data is
now invalid.  I have tried the following:

fireTableRowsDeleted
fireTableChanged
fireTableDataChanged

and so on....

all to no avail.

So what is the correct way of notifying table and/or model that an
element has been removed frm the data Vector?

Answers greatly appreciated...

Phillipus
Technical Details
Message-ID: <36518E57.949D9FA0@bangor.ac.uk>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,e8d4105e0b153509,start
X-Google-Attributes: gid111d45,public
From: Phillipus <p.beauvoir@bangor.ac.uk>
Subject: How to delete a row in a JTable
Date: 1998/11/17
Message-ID: <36518E57.949D9FA0@bangor.ac.uk>
X-Deja-AN: 412786181
Content-Transfer-Encoding: 7bit
Organization: Another Netscape Collabra Server User
Content-Type: text/plain; charset=us-ascii
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing