Results 1 to 2 of 2
  1. #1
    darrell22 is offline Member
    Join Date
    Feb 2010
    Posts
    13
    Rep Power
    0

    Default Fixed rowtype in DefaultTableModel or other class?

    Dear Experts,


    Most of my background is in the database area, and
    lately, I've been looking at the DefaultTableModel.


    This is a good simple example.
    All the data in the example is String.
    Create DefaultTableModel : JTable Model¬*¬ę¬*Swing¬*¬ę¬*Java Tutorial


    I want to have a standard row, that has columns of a set datatype.

    So, the data in column 1 would always be say, String.
    And data in column 2 would always be integer.
    Data in column 3 would always be Date. Etc.
    Just like in databases. Rigorously enforced.

    My initial thoughts were to first declare a class for a row type.

    public class rowForTable
    { String col1Str = "" ;
    int col2Int = 0 ;
    Date col3theDate = new Date();
    } ;


    Then, during insert, populate with the correct data, and
    insert them into the DefaultTableModel object.


    ----


    Looking at the DefaultTableModel docs, this seems to be possible.
    DefaultTableModel (Java Platform SE 7 )


    addRow(Object[] rowData)
    Adds a row to the end of the model.


    public void insertRow(int row,
    Vector rowData)


    ----

    However, when I look at this example,

    java - Jtable with different types of cells depending on data type - Stack Overflow

    this data changes from row to row.

    private Object[][] data = {
    {"String", "I'm a string"},
    {"Date", new Date()},
    {"Integer", new Integer(123)},
    {"Double", new Double(123.45)},
    {"Boolean", Boolean.TRUE}};

    ----

    Is it possible to do what I'm thinking with the DefaultTableModel?
    Or, is there perhaps a better class to do what I'm thinking of?


    Does anyone know of a simple example they can refer me to?



    Thanks a lot!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,371
    Rep Power
    21

    Default Re: Fixed rowtype in DefaultTableModel or other class?

    If you've got a model class that represents your data you should be able to write your own TableModel (off AbstractTableModel, to avoid all the listener stuff) to simply deal with a List<YourModel>.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Removing rows from DefaultTableModel
    By the reporter in forum AWT / Swing
    Replies: 4
    Last Post: 08-01-2010, 04:41 PM
  2. DefaultTableModel vs AbstractTableModel
    By chyrl in forum AWT / Swing
    Replies: 4
    Last Post: 05-02-2010, 12:10 PM
  3. DefaultTableModel problem
    By stylian in forum AWT / Swing
    Replies: 2
    Last Post: 10-31-2009, 05:23 PM
  4. RequestFocus on DefaultTableModel Particular Cell
    By yernikumar in forum AWT / Swing
    Replies: 1
    Last Post: 03-01-2009, 05:58 PM
  5. Insert oracle rowtype
    By Faheem_Ahmed in forum New To Java
    Replies: 0
    Last Post: 02-28-2009, 08:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •