public abstract class DataColumn
extends Object
ListStore
or
TreeStore
you use an array of DataColumns to indicate the data type
that each column of the model is to hold.
The types you can store in a TreeModel is somewhat deliberately constrained to those that make sense when it comes time to present them. In almost every case you will be presenting textual data and so will want a DataColumnString to store it in. And while there are DataColumnInteger and DataColumnLong, if will often find yourself doing formatting on that data (currency, dates, etc) on the Java side before pushing it into the model and so will end up using a DataColumnString anyway.
Keep in mind that you do not need to store all your application data
in the TreeModel. After all, you already have a perfectly sound (and
infinitely more powerful) means to model and represent your data: the Java
language you're working from in the first place. You only need to push data
into a TreeModel that will be directly displayed by a TreeView or that will
be used to help manage that display (such as using a numeric type as an
auxiliary sorting index for textual columns that otherwise wouldn't order
properly). Quite frequently, however, you will want to take a selection or
activation event from the TreeView and get back to the Java object that
where the displayed data came from in the first place. This is the role of
the other significant column type, DataColumnReference
.
The optimal way to use this class is as follows. First declare variables to represent each column (they can be local variables but often you end up needing to access them from code outside the declaring method, so they usually end up as private instance variables).
... private final DataColumnString name; private final DataColumnString dob; // date of birth private final DataColumnLong dobSort; private final DataColumnBoolean useful; // is this person valued, or a waste of oxygen?and then instantiate them;
... name = new DataColumnString(); dob = new DataColumnString(); dobSort = new DataColumnLong(); useful = new DataColumnBoolean();It is important to choose useful names for these variables; you'll be using them to conveniently identify the columns in later code (some people even go so far as to use all capitals to make them stand out, or use a suffix like
Column
, though neither are really necessary). You then
make an array of them and use that for the ListStore constructor:
final DataColumn[] types; final ListStore model; types = new DataColumn[] { name, dob, dobSort, useful } model = new ListStore(types);You can rather nicely do this all at once:
... model = new ListStore(new DataColumn[] { name = new DataColumnString(), dob = new DataColumnString(), dobSort = new DataColumnInteger(), useful = new DataColumnBoolean() });which is ultimately the API we had in mind for you to use! (Important: do not do the following:
... types = new DataColumn[] { new DataColumnString(), new DataColumnString(), new DataColumnInteger(), new DataColumnBoolean() }; new ListStore(types);as this means you can only refer to each column as
types[2]
thus defeating the whole idea of having a meaningfully named handle to the
column that each instance of this class represents. You will need
those comprehensible names later).
See the list of subclasses of DataColumn to see the current set of available column types.