Skip to main content

[FYI] ColumnFactory and JXTable

No replies
Anonymous

Hi folks,

in the process of bug-fixing on ColumnFactory and doc cleanup (yeah...
occasionally even me is doing some raw doc'ing ) I remembered an
older request to have a better support for configuring JXTable with a
custom ColumnFactory, so I happily jumped into it.

Here's the state of affairs:

Since "ever" (relative and imprecise time measurement :-) JXTable
guarantees to pass all TableColumn creation and configuration through
its associated ColumnFactory. In principle, this allowed a single entry
point for consistent column config. The current default implementation
doesn't do much, a richer implementation (making use of the
MetaDataProvider et al got buried with the burial of the "old" binding)

Nevertheless, the recommended SwingX-way to customize the default setup
of column properties is to implement a custom ColumnFactory and install
this factory. Until now, this could be done easily only for the
application-wide shared factory, a per-table factory required
subclassing of the JXTable as well (shame on me ;-)

Now the JXTable has a full-fledged bound property columnFactory to allow
per-instance setting of a custom factory, if needed.

Taking the column config from Patrick's (hope you don't mind, I'm too
lazy to roll my own!) example in SwingLabs-demos (SimpleJXTableDemo):

The example simulates a typical load of data form a database with
typical columnNames (which are copied by the default ColumnFactory into
the column's headerValue): uppercase letters with underscores
representing spaces. The task is to beautify those names while keeping
the old as identifier.

the Swing-way: manually loop through the columns after they had
been created.

<br />
for (Iterator iter = jxTable.getColumns(true).iterator();<br />
iter.hasNext();) {<br />
     TableColumnExt column = (TableColumnExt) iter.next();<br />
     column.setIdentifier(column.getIdentifier());<br />
     // ...and now change the title<br />
     String title = column.getTitle();<br />
     title = title.substring(0,1).toUpperCase() +<br />
title.substring(1).toLowerCase();<br />
     column.setTitle(title.replace('_', ' ');<br />
}</p>
<p>

The SwingX-way: implement a ColumnFactory to do it

</p>
<p>ColumnFactory factory = new ColumnFactory() {</p>
<p>     @Override<br />
     public void configureTableColumn(TableModel model,<br />
             TableColumnExt columnExt) {<br />
         super.configureTableColumn(model, columnExt);<br />
         columnExt.setIdentifier(columnExt.getIdentifier());<br />
         // ...and now change the title<br />
         String title = columnExt.getTitle();<br />
         title = title.substring(0, 1).toUpperCase()<br />
                 + title.substring(1).toLowerCase();<br />
         columnExt.setTitle(title.replace('_', ' '));<br />
     }</p>
<p>};</p>
<p>

Install application-wide:

<br />
ColumnFactory.setInstance(factory);<br />

Install on a particular table instance:

[code]
xTable.setColumnFactory(factory);
[code]

In both cases, all auto-created columns (which happens if the
autocreateColumns property is true which is the default) in JXTable will
be configured automagically after setModel and after receiving a
structureChanged.

Enjoy or condemn Looking forward to comments!

Have a nice weekend (here in Germany it will be a long one, Monday is
holiday)
Jeanette

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net