The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.table  [62 examples] > Rows  [8 examples]

e908. Appending a Row to a JTable Component

To add a row of data to a JTable component, you need to add it to its table model. A simple implementation of a table model that supports appending row data is DefaultTableModel. By default, a table uses a DefaultTableModel.
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    
    // Create a couple of columns
    model.addColumn("Col1");
    model.addColumn("Col2");
    
    // Append a row
    model.addRow(new Object[]{"v1", "v2"});
    // there are now 2 rows with 2 columns
    
    // Append a row with fewer values than columns.
    // The left-most fields in the new row are populated
    // with the supplied values (left-to-right) and fields
    // without values are set to null.
    model.addRow(new Object[]{"v1"});
    // there are now 3 rows with 2 columns
    
    // Append a row with more values than columns.
    // The extra values are ignored.
    model.addRow(new Object[]{"v1", "v2", "v3"});
    // there are now 4 rows with 2 columns

 Related Examples
e907. Getting the Number of Rows and Columns in a JTable Component
e909. Inserting a Row in a JTable Component
e910. Removing a Row from a JTable Component
e911. Moving a Row in a JTable Component
e912. Copying a Row or Column in a JTable Component
e913. Setting the Height of a Row in a JTable Component
e914. Shading Rows and Columns in a JTable Component

See also: Cells    Column Heads    Columns    Editing    Events    Layout    Scrolling    Selection    Sorting    Table Model    Tool Tips   


© 2002 Addison-Wesley.