Hi,
what would be the best way to return a "default" value for the following.
I have a vector(1) holding a vector(2) of objects(3), in this case used for
a jtable.
the vector (1) is attached to the Jtable to allow table updates
the vector(2) holds each row
the Object(3) the value
so at its most basic form it is:
a vector(new Object[n])) to represent a single row.
for code re-use, I want to get a vector(2) of objects (3), which would
represent a table row.
then generate an empty record from it.
This would require looking at each object type, and generating a default
value.
so if it was a string i would set the object to new string("")
and integer ,would be new Integer(0)
and so forth.
obviously this could translate in to rather a large code body,
of (if instance of)
is there a better way of doing it?
Steve
Bjorn Abelli - 06 Nov 2005 11:44 GMT
"steve" wrote...
> so if it was a string i would set the object to
> new string("") and integer,would be new Integer(0)
[quoted text clipped - 6 lines]
>
> is there a better way of doing it?
IMHO that depends on three things:
1. what you consider being the default values
of arbitrary objects,
What would the default value of e.g. an instance of class "Xyz" be?
2. what you really want to *use* the values for.
Either you really *need* to know the values for each class you're using, or
you could possibly narrow the selection to just two types, numeric and
others, and put a "0" in for numeric values and an empty string in for
others.
You could also use reflection to get it somewhat more "generic", i.e. that
it's possible to create some kind of "default value" for every type of
class.
3. why you want this construction.
From what you wrote I can't see the purpose of that kind of construction.
My own approach would probably be to not bother, as null values shows up as
ampty strings in a JTable anyway.
What I *would* do though, is to not use the Vector(Vector) approach at all,
but instead to construct a backing TableModel.
// Bjorn A
zero - 06 Nov 2005 12:58 GMT
> Hi,
>
[quoted text clipped - 30 lines]
>
> Steve
I think what you're looking for is the reflection API.
http://java.sun.com/docs/books/tutorial/reflect/object/create.html