🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: How to delete a row in a JTable

#12667
From: Phillipus
Date: Thu, 19 Nov 1998 00:00
92 lines
3078 bytes
Answered it myself!

Thus:

fireTableRowsDeleted(row1, row2);

is the correct method to call but it causes a de-selection of row(s) in the
table.  This then fires a selection event to any registered selection
listeners with a row value of -1 (meaning no row selected).  I had not
trapped a -1 and so I was referencing a Vector element with a -1 index and
getting an indexOutOfBounds error.  Oops.

Phillipus



Phillipus wrote:

> 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: <3653F0C7.9FA4D7B0@bangor.ac.uk>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <36518E57.949D9FA0@bangor.ac.uk>

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