Setting a default cell editor based on the column type

Discussion in 'Java' started by Lizard, Dec 21, 2006.

  1. Lizard

    Lizard Guest

    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
     
    Lizard, Dec 21, 2006
    #1
    1. Advertising

  2. Lizard

    Daniel Dyer Guest

    On Thu, 21 Dec 2006 20:38:35 -0000, Lizard <> 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
     
    Daniel Dyer, Dec 21, 2006
    #2
    1. Advertising

  3. Lizard

    Lizard Guest

    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
     
    Lizard, Dec 21, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison
    Replies:
    1
    Views:
    5,583
  2. news.rcn.com
    Replies:
    1
    Views:
    9,675
    Chris Smith
    Jul 12, 2004
  3. Stylus Studio
    Replies:
    0
    Views:
    683
    Stylus Studio
    Aug 3, 2004
  4. CJ
    Replies:
    1
    Views:
    1,574
    Andrew Thompson
    Oct 29, 2004
  5. RJ
    Replies:
    1
    Views:
    244
    Eliyahu Goldin
    Feb 17, 2005
Loading...

Share This Page