Setting a default cell editor based on the column type

L

Lizard

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?
 
D

Daniel Dyer

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.
 
L

Lizard

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top