Article View: borland.public.jbuilder.java.swing
Article #12029Re: Adding Columns to Jtable at Runtime
From: johnmo
Date: Thu, 25 Feb 1999 00:00
Date: Thu, 25 Feb 1999 00:00
46 lines
2339 bytes
2339 bytes
In article <7aqmtu$61b$1@nnrp1.dejanews.com>,
apatil@digit.com wrote:
> Hi John
>
> I tried adding column to the table at runtime. But the column which
> is newly added becomes non functional without any data and also I cannot
> edit the cells in the column .
> I think the problem lies with setting the model index for the column. If I set
> a modelIndex which is already existing to the new column then everything works
> fine but data in the columns is repeated.
>
> Please help me in solving this problem . Also possibly send the code
> where u have implemnted this.
>
> Ajay
>
Hi,
I set things up so that the underlaying dataModel contained the data for the
additional column. I then rebuilt the columns like so:-
TableColumnModel cm = dataTable.getColumnModel();
while (cm.getColumnCount() > 0)
cm.removeColumn(cm.getColumn(0));
for(int col=0;col<dataModel.getColumnCount();col++)
{
dataTable.addColumn(new TableColumn(col));
}
The col variable in the TableColumn constructor associates a column with the
column inside the dataModel, so I thats why I put the data into the dataModel
first.
The dataModel extends AbstractTableModel, which has a method
public boolean isCellEditable(int rowIndex,int columnIndex)
which is called by the table to allow editing to proceed. So if you return
true you can edit data.
Hope this helps
Cheers,
John
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
Technical Details
Message-ID:
<7b3om0$tl3$1@nnrp1.dejanews.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<36B9822D.34CDC91A@sheffield.ac.uk> <79cbjd$ivl$1@nnrp1.dejanews.com> <7aqmtu$61b$1@nnrp1.dejanews.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,3f3bbfe523f452fc X-Google-Attributes: gid111d45,public From: johnmo@pcdocs.com Subject: Re: Adding Columns to Jtable at Runtime Date: 1999/02/25 Message-ID: <7b3om0$tl3$1@nnrp1.dejanews.com> X-Deja-AN: 448311705 References: <36B9822D.34CDC91A@sheffield.ac.uk> <79cbjd$ivl$1@nnrp1.dejanews.com> <7aqmtu$61b$1@nnrp1.dejanews.com> To: apatil@digit.com X-Http-Proxy: 1.0 TEMPHOST, 1.0 x10.dejanews.com:80 (Squid/1.1.22) for client 208.153.28.6 Organization: Deja News - The Leader in Internet Discussion X-Article-Creation-Date: Thu Feb 25 15:02:31 1999 GMT Newsgroups: borland.public.jbuilder.java.swing X-Http-User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows NT)