This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Dynamically redrawing columns in TableViewer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Dynamically redrawing columns in TableViewer" Watch "Dynamically redrawing columns in TableViewer" New topic
Author

Dynamically redrawing columns in TableViewer

J Arshad
Greenhorn

Joined: Sep 07, 2004
Posts: 19
Hi,

Does anyone know how to reset column names, properties, etc dynamically. For instance, my application allows the user to add rows as well as columns. When a new row is added to the input, it appears in the table viewer upon refresh(). But what do I have to do to force a new column to appear when the user chooses to add one.

Currently, I'm doing these steps:

1. Making a call to TableViewer::setColumnProperties() with the new column names.
2. Reformatting TableViewer::getTable() to include new columns.
3. Resetting TableViewer::setCellEditors() to create the correct number of editors as a result of adding a column.

But having no joy with this:-(. If I step through the code, it has the new column name both in table viewer's properties as well in the column names of TableViewer::getTable(), but it just wouldn't redraw the table.

Have spent too much time on this. Any help would be greatly appreciated!

Jay
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The general approach is to add the column or row to the TableModel, either an AbstractTableModel or its subclass DefaultTableModel, and then tell the model to update itself using methods available in each api. The JTable api has methods for setting default editors and renderers based on class type. Or you can add a specific editor or renderer to each new column as you add it to the model.
J Arshad
Greenhorn

Joined: Sep 07, 2004
Posts: 19
Thanks!

I'm actually using SWT and not Swing. So setInput() is the way you would pass a model to the table ad then render it using an ITableLabelProvider implementation.

My problem lies in the fact that I'm able to change rows dynamically (adding a new row is adding another data set to the model). But adding a new column seems to be different. Even though the model has all the right columns in the memory (after adding), the actual table doesn't get refreshed/redrawn. I have tried TableViewer::refresh() as well as TableViewer::getTable().redraw().

So if anyone has done this in SWT, it'd be a great help.

Jay.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Dynamically redrawing columns in TableViewer
 
Similar Threads
Runtime Creating Tables
Any SWT gurus out there?
Dynamically build a javaBean
JTable related problems
Focuslost listener for TableViewer