Home > Archive > Java Help > December 2006 > Setting a default cell editor based on the column type
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Setting a default cell editor based on the column type
|
|
| Lizard 2006-12-21, 7:08 pm |
| OK.
I have a tabbed form.
Each tab has a (different) table on it.
Each table is based on a .CSV file.
For each table, I want to use a custom cell editor for integers, and
another for floating points, and the generic cell editor for
everything else. I've written and tested the two custom cell editors.
The tableModel for all the tables is an ArrayList of ArrayLists of
Objects.
For each specific table, I have two Vectors, one containing a list of
Integer columns, one containing a list of Floating Point columns.
As I read the CSV, I use a string tokenizer to break the row up, then
add each element to an ArrayList<Object>, then add the
ArrayList<Object> to an ArrayList<ArrayList<Object>>, then pass that
to the TableModel. This all works fine.
As each item is added, I check the position in the token list against
the Vectors which contain the Float or Integer colummns. IOW, if we're
at item 3, and IntegerVector.contains(3), I cast the data to Integer
before adding it. If the column isn't in either IntegerVector or
DoubleVector, it is cast to a String.
Thus, every column is cast to either Integer, Double, or String.
Then I have:
theTable.setDefaultEditor(Double.class, new
FloatingPointCellEditor());
theTable.setDefaultEditor(Integer.class, new IntegerCellEditor());
And no errors are thrown.
However, it doesn't work. The default editor is used for all cells.
The code
theTable.getColumnModel().getColumn(x).setCellEditor(newIntegerCellEditor());
does work, but it's easier for me to create a vector of columns than
to hard-code in the column editors one column at a time.
I guess my main question is, if I define my tableModel as
ArrayList<ArrayList<Object>>, and make sure all the Objects in, say,
colum 3 are Doubles, does this mean that the JTable automagically
knows that data is of type Double?
--
Personal Blog:http://www.xanga.com/lizard_sf
Ranty Political Blog:http://www.pontification.com
| |
| Daniel Dyer 2006-12-21, 7:08 pm |
| On Thu, 21 Dec 2006 20:38:35 -0000, Lizard <lizard@mrlizard.com> wrote:
> For each table, I want to use a custom cell editor for integers, and
> another for floating points, and the generic cell editor for
> everything else. I've written and tested the two custom cell editors.
>
> The tableModel for all the tables is an ArrayList of ArrayLists of
> Objects.
>
> For each specific table, I have two Vectors, one containing a list of
> Integer columns, one containing a list of Floating Point columns.
>
> As I read the CSV, I use a string tokenizer to break the row up, then
> add each element to an ArrayList<Object>, then add the
> ArrayList<Object> to an ArrayList<ArrayList<Object>>, then pass that
> to the TableModel. This all works fine.
>
> As each item is added, I check the position in the token list against
> the Vectors which contain the Float or Integer colummns. IOW, if we're
> at item 3, and IntegerVector.contains(3), I cast the data to Integer
> before adding it. If the column isn't in either IntegerVector or
> DoubleVector, it is cast to a String.
>
> Thus, every column is cast to either Integer, Double, or String.
>
> Then I have:
> theTable.setDefaultEditor(Double.class, new
> FloatingPointCellEditor());
> theTable.setDefaultEditor(Integer.class, new IntegerCellEditor());
>
> And no errors are thrown.
>
> However, it doesn't work. The default editor is used for all cells.
>
> The code
>
> theTable.getColumnModel().getColumn(x).setCellEditor(newIntegerCellEditor());
> does work, but it's easier for me to create a vector of columns than
> to hard-code in the column editors one column at a time.
>
> I guess my main question is, if I define my tableModel as
> ArrayList<ArrayList<Object>>, and make sure all the Objects in, say,
> colum 3 are Doubles, does this mean that the JTable automagically
> knows that data is of type Double?
What class are you using for the TableModel? Is is one you have
implemented yourself? You need to implement the getColumnClass(int
column) method to return the appropriate Class object.
I typically implement this by having an array of Class objects as a static
field of the table model class and then just return the Class at the
appropriate position when getColumnClass is invoked. But you could
equally use your approach of a Vector (a set would be better) that
contains the column indices.
Dan.
--
Daniel Dyer
http://www.uncommons.org
| |
| Lizard 2006-12-21, 7:08 pm |
| On Thu, 21 Dec 2006 21:05:22 -0000, "Daniel Dyer" <"You don't need
it"> wrotC:DRIVE_E
>What class are you using for the TableModel? Is is one you have
>implemented yourself?
Yup. At the moment, it's more-or-less default behaviour, but I know
I'll need to add more later, so I figured I'd learn the class now when
my needs were simple, and it can grow in functionality w/the app.
> You need to implement the getColumnClass(int
>column) method to return the appropriate Class object.
>
Ah ha. Thanks. That helps a LOT.
>I typically implement this by having an array of Class objects as a static
>field of the table model class and then just return the Class at the
>appropriate position when getColumnClass is invoked. But you could
>equally use your approach of a Vector (a set would be better) that
>contains the column indices.
>
Thank you for your help. i will look into implementing something like
that.
--
Personal Blog:http://www.xanga.com/lizard_sf
Ranty Political Blog:http://www.pontification.com
|
|
|
|
|