# Class DefaultTableModel

java.lang.Object
com.codename1.ui.table.AbstractTableModel
com.codename1.ui.table.DefaultTableModel

All Implemented Interfaces:
  `TableModel`

public class DefaultTableModel extends AbstractTableModel

A default implementation of the table model based on a two dimensional array.

    Form hi = new Form("Table", new BorderLayout());
    TableModel model = new DefaultTableModel(new String[] {"Col 1", "Col 2", "Col 3"}, new Object[][] {
        {"Row 1", "Row A", "Row X"},
        {"Row 2", "Row B can now stretch", null},
        {"Row 3", "Row C", "Row Z"},
        {"Row 4", "Row D", "Row K"},
        }) {
            public boolean isCellEditable(int row, int col) {
                return col != 0;
            }
        };
    Table table = new Table(model) {
    @Override
        protected Component createCell(Object value, int row, int column, boolean editable) { // (1)
            Component cell;
            if(row == 1 && column == 1) { // (2)
                Picker p = new Picker();
                p.setType(Display.PICKER_TYPE_STRINGS);
                p.setStrings("Row B can now stretch", "This is a good value", "So Is This", "Better than text field");
                p.setSelectedString((String)value); // (3)
                p.setUIID("TableCell");
                p.addActionListener((e) -> getModel().setValueAt(row, column, p.getSelectedString())); // (4)
                cell = p;
            } else {
                cell = super.createCell(value, row, column, editable);
            }
            if(row > -1 && row % 2 == 0) { // (5)
                // pinstripe effect
                cell.getAllStyles().setBgColor(0xeeeeee);
                cell.getAllStyles().setBgTransparency(255);
            }
            return cell;
        }
    @Override
        protected TableLayout.Constraint createCellConstraint(Object value, int row, int column) {
            TableLayout.Constraint con =  super.createCellConstraint(value, row, column);
            if(row == 1 && column == 1) {
                con.setHorizontalSpan(2);
            }
            con.setWidthPercentage(33);
            return con;
        }
    };
    hi.add(BorderLayout.CENTER, table);
    hi.show();

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `DefaultTableModel(String[] columnNames, Object[][] data)`
  Constructs a new table with a 2 dimensional array for row/column data
  `DefaultTableModel(String[] columnNames, Object[][] data, boolean editable)`
  Constructs a new table with a 2 dimensional array for row/column data

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addDataChangeListener(DataChangedListener d)`
  Adds a listener to the data changed event
  `void`
  `addRow(Object... row)`
  Adds the given row to the table data
  `int`
  `getColumnCount()`
  Returns the number of columns in the table
  `String`
  `getColumnName(int i)`
  Returns the name of the column at the given offset
  `int`
  `getRowCount()`
  Returns the number of rows in the table
  `Object`
  `getValueAt(int row, int column)`
  Returns the value of the cell at the given location
  `void`
  `insertRow(int offset, Object... row)`
  Inserts the given row to the table data at the given offset
  `boolean`
  `isCellEditable(int row, int column)`
  Returns true if the cell at the given location is an editable cell
  `void`
  `removeDataChangeListener(DataChangedListener d)`
  Removes a listener to the data changed event
  `void`
  `removeRow(int offset)`
  Removes the given row offset from the table
  `void`
  `setValueAt(int row, int column, Object o)`
  Sets the value of the cell at the given location

  ### Methods inherited from class AbstractTableModel

  `getCellType, getMultipleChoiceOptions, getValidationConstraint, getValidator, setValidator`

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### DefaultTableModel

    public DefaultTableModel(String[] columnNames, Object[][] data)

    Constructs a new table with a 2 dimensional array for row/column data

    ###### Parameters

    - `columnNames`: the names of the columns

    - `data`: the data within the table

  - ### DefaultTableModel

    public DefaultTableModel(String[] columnNames, Object[][] data, boolean editable)

    Constructs a new table with a 2 dimensional array for row/column data

    ###### Parameters

    - `columnNames`: the names of the columns

    - `data`: the data within the table

    - `editable`: indicates whether table cells are editable or not by default

    ###### See also

    - #isCellEditable(int, int)

- ## Method Details

  - ### getRowCount

    public int getRowCount()

    Returns the number of rows in the table

    ###### Returns

    the number of rows in the table

  - ### getColumnCount

    public int getColumnCount()

    Returns the number of columns in the table

    ###### Returns

    the number of columns in the table

  - ### getColumnName

    public String getColumnName(int i)

    Returns the name of the column at the given offset

    ###### Parameters

    - `i`: the offset for the column name

    ###### Returns

    name to display at the top of the table

  - ### isCellEditable

    public boolean isCellEditable(int row, int column)

    Returns true if the cell at the given location is an editable cell

    ###### Parameters

    - `row`: the cell row

    - `column`: the cell column

    ###### Returns

    true if the cell at the given location is an editable cell

  - ### getValueAt

    public Object getValueAt(int row, int column)

    Returns the value of the cell at the given location

    ###### Parameters

    - `row`: the cell row

    - `column`: the cell column

    ###### Returns

    the value of the cell at the given location

  - ### setValueAt

    public void setValueAt(int row, int column, Object o)

    Sets the value of the cell at the given location

    ###### Parameters

    - `row`: the cell row

    - `column`: the cell column

    - `o`: the value of the cell at the given location

  - ### addDataChangeListener

    public void addDataChangeListener(DataChangedListener d)

    Adds a listener to the data changed event

    ###### Parameters

    - `d`: the new listener

  - ### removeDataChangeListener

    public void removeDataChangeListener(DataChangedListener d)

    Removes a listener to the data changed event

    ###### Parameters

    - `d`: the listener to remove

  - ### addRow

    public void addRow(Object... row)

    Adds the given row to the table data

    ###### Parameters

    - `row`: array or row items, notice that row.length should match the column count exactly!

  - ### insertRow

    public void insertRow(int offset, Object... row)

    Inserts the given row to the table data at the given offset

    ###### Parameters

    - `offset`: position within the table that is 0 or larger yet smaller than the row count

    - `row`: array or row items, notice that row.length should match the column count exactly!

  - ### removeRow

    public void removeRow(int offset)

    Removes the given row offset from the table

    ###### Parameters

    - `offset`: position within the table that is 0 or larger yet smaller than the row count