🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
2 messages
2 total messages Started by Zoran Pjevic Thu, 11 Dec 2003 14:36
How to permanantly set a custom CellRenderer to a jTable?
#1485
Author: Zoran Pjevic
Date: Thu, 11 Dec 2003 14:36
42 lines
2293 bytes
Following situtation:

I am using a jtable with a custom tablemodel, custom cellrenderer.

The table displays dynamic data coming from an external source.

I attach the the cell renderer to the table and set up the columns
widths as follows:

MyTableModel tableModel = new MyTableModel();
JTable table = new JTable(tableModel);
JViewport tableViewport = jScrollPane_PcmTelemetry.getViewport();
tableViewport.add(table, null);

for (int i=0; i < table.getColumnCount(); i++) {
      table.setDefaultRenderer(jTablePcmTelemetry.getColumnClass(i),
new MyCellRenderer());
}

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumnModel tableColumnModel = table.getColumnModel();
for (int i=0; i < tableColumnModel.getColumnCount(); i++) {
	tableColumnModel.getColumn(i).setPreferredWidth(35);
}

But then when new data comes the TableModel calls either
 fireTableStructureChanged(), fireTableDataChanged() or
fireTableRowsUpdated(0, size-1)
depending on what has changed in the data.

After this I loose the CellRenderers and the Width of the Columns, ie.
there set back to the default ones.

How can I set a Custom Cell Renderer permanently or set the width of
the columns permanently so that they 
will survive any of the previous mentioned events?

I'll be grateful for any help on this.

Regards,
Zoran
 
Re: How to permanantly set a custom CellRenderer to a jTable?
#1488
Author: Paul Furbacher
Date: Thu, 11 Dec 2003 20:00
40 lines
2436 bytes
Zoran Pjevic wrote:

> But then when new data comes the TableModel calls either
> fireTableStructureChanged(), fireTableDataChanged() or
> fireTableRowsUpdated(0, size-1) ....
> 
> After this I loose the CellRenderers and the Width of the Columns, ie.
> there set back to the default ones.
> 
> How can I set a Custom Cell Renderer permanently or set the width of
> the columns permanently so that they 
> will survive any of the previous mentioned events?

You'll notice that JTable's implementation recreates the
columns based on the value of "autoCreateColumnsFromModel".
The field is set to true in the constructor.

As I see it, you have two choices.

Try setting that field to false via its accessor.

Or, if that doesn't work to your satisfaction,
extend JTable and override the "tableChanged()"
method to do what you want.


-- 


Paul Furbacher (TeamB)

   Save time, search the archives:
        http://www.borland.com/newsgroups/ngsearch.html

   Is it in Joi Ellis's Faq-O-Matic?
        http://www.visi.com/~gyles19/fom-serve/cache/1.html

   Finally, please send responses to the newsgroup only.
   That means, do not send email directly to me.
   Thank you.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads