Create JTable from DefaultTableModel - Java Swing

Java examples for Swing:JTable Model

Description

Create JTable from DefaultTableModel

Demo Code

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Main {

  public void main(String[] argv) {
    //from  w ww  .ja  v a 2  s  .co  m

    // Add data
    DefaultTableModel model = new DefaultTableModel();
    model.addColumn("A");
    model.addColumn("B");
    model.addColumn("C");
    model.addRow(new Object[] { "A1", "B1", "C1" });
    model.addRow(new Object[] { "A2", "B2", "C2" });
    model.addRow(new Object[] { "A3", "B3", "C3" });
    
    JTable table = new JTable(model);
  }
}

Related Tutorials