Article View: borland.public.jbuilder.java.swing
Article #12245Re: Adding Columns to Jtable at Runtime
From: johnmo
Date: Thu, 04 Feb 1999 00:00
Date: Thu, 04 Feb 1999 00:00
44 lines
2193 bytes
2193 bytes
In article <36B9822D.34CDC91A@sheffield.ac.uk>,
Anne Nortcliffe <cop94aln@sheffield.ac.uk> wrote:
> How can I add and populate a new column to a JTable at runtime without?
> I'd also like the contents of some of the cells to reflect the the value
> of other components such as text boxes etc. that may lie in other panels
> etc... any ideas/comments would be greatly appreciated...
>
>
I'm not sure if this is the right way, but I used a class derived from
AbstractTableModel to store the column names i.e
AbstractTableModel.setColumnName
This class was then used in the constructor of JTable ie new
JTable(TableModel)
My implementation of setColumnName also fired fireTableRowsUpdated(-1,column)
as well.
To get the columns to appear I had then had to remove and add all the columns
again, otherwise the new column titles wouldn't appear
dataTable is of type JTable, dataModel is the class derived from
AbstractTableModel
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));
}
To actually add to the column I used AbstractTableModel.setValueAt and inside
the implementation used fireTableCellUpdated
Hope this help.
cheers,
John
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
Technical Details
Message-ID:
<79cbjd$ivl$1@nnrp1.dejanews.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<36B9822D.34CDC91A@sheffield.ac.uk>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/04 Message-ID: <79cbjd$ivl$1@nnrp1.dejanews.com> X-Deja-AN: 440585179 References: <36B9822D.34CDC91A@sheffield.ac.uk> X-Http-Proxy: 1.0 TEMPHOST, 1.0 x8.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 04 14:41:51 1999 GMT Newsgroups: borland.public.jbuilder.java.swing X-Http-User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows NT)