Creating simple JTable using AbstractTableModel : JTable Model « Swing « Java Tutorial






import java.awt.Dimension;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class Main extends JFrame {
  private JTable m_simpleTable;

  private SimpleTableModel m_simpleTableModel;

  public Main() {
    Vector dummyMacData = new Vector(10, 10);
    dummyMacData.addElement(new Data(new Integer(100), "A", "1","C", "E"));
    dummyMacData.addElement(new Data(new Integer(105), "R", "2","S", "E"));
    m_simpleTableModel = new SimpleTableModel(dummyMacData);
    m_simpleTable = new JTable(m_simpleTableModel);
    JScrollPane scrollPane = new JScrollPane(m_simpleTable);
    getContentPane().add(scrollPane);
  }

  public static void main(String[] arg) {
    Main m = new Main();

    m.setVisible(true);
    m.setSize(new Dimension(600, 300));
    m.validate();
  }

  class SimpleTableModel extends AbstractTableModel {
    public String[] m_colNames = { "A", "B", "C","D", "E" };

    public Class[] m_colTypes = { Integer.class, String.class, String.class, String.class,
        String.class };

    Vector m_macDataVector;

    public SimpleTableModel(Vector macDataVector) {
      super();
      m_macDataVector = macDataVector;
    }
    public int getColumnCount() {
      return m_colNames.length;
    }
    public int getRowCount() {
      return m_macDataVector.size();
    }
    public void setValueAt(Object value, int row, int col) {
      Data macData = (Data) (m_macDataVector.elementAt(row));

      switch (col) {
      case 0:
        macData.setA((Integer) value);
        break;
      case 1:
        macData.setB((String) value);
        break;
      case 2:
        macData.setC((String) value);
        break;
      case 3:
        macData.setD((String) value);
        break;
      case 4:
        macData.setE((String) value);
        break;
      }
    }

    public String getColumnName(int col) {
      return m_colNames[col];
    }

    public Class getColumnClass(int col) {
      return m_colTypes[col];
    }
    public Object getValueAt(int row, int col) {
      Data macData = (Data) (m_macDataVector.elementAt(row));

      switch (col) {
      case 0:
        return macData.getA();
      case 1:
        return macData.getB();
      case 2:
        return macData.getC();
      case 3:
        return macData.getD();
      case 4:
        return macData.getE();
      }

      return new String();
    }
  }

}

class Data {
  private Integer a;

  private String b;

  private String c;

  private String d;

  private String e;

  public Data() {
  }

  public Data(Integer aa, String bb, String cc, String dd, String ee) {
    a = aa;
    b = bb;
    c = cc;
    d = dd;
    e = ee;
  }

  public Integer getA() {
    return a;
  }

  public String getB() {
    return b;
  }

  public String getC() {
    return c;
  }

  public String getD() {
    return d;
  }

  public String getE() {
    return e;
  }

  public void setA(Integer aa) {
    a = aa;
  }

  public void setB(String macName) {
    b = macName;
  }

  public void setC(String cc) {
    c = cc;
  }

  public void setD(String dd) {
    d = dd;
  }

  public void setE(String ee) {
    e = ee;
  }
}








14.59.JTable Model
14.59.1.Create DefaultTableModel
14.59.2.Creating simple JTable using AbstractTableModel
14.59.3.Extending AbstractTableModelExtending AbstractTableModel
14.59.4.Creates tables that allow rows and columns to be added or deleted
14.59.5.Set table valueSet table value
14.59.6.Add logic to get value methodAdd logic to get value method
14.59.7.Subclass AbstractTableModel to reuse an existing data structure: Treat an array as an AbstractTableModelSubclass AbstractTableModel to reuse an existing data structure: Treat an array as an AbstractTableModel
14.59.8.Sparsely Populated Table ModelSparsely Populated Table Model
14.59.9.Listening to JTable Events with a TableColumnModelListenerListening to JTable Events with a TableColumnModelListener
14.59.10.The Calendar program.The Calendar program.
14.59.11.Sample Sorting Table ModelSample Sorting Table Model
14.59.12.Add columns to a table through DefaultTableModel
14.59.13.Insert a row to a table through DefaultTableModel
14.59.14.Insert a row to a table through DefaultTableModel at specified row
14.59.15.Append a row to a table through DefaultTableModel at specified row
14.59.16.Remove the first row from a table with DefaultTableModel
14.59.17.Remove the last row from a table with DefaultTableModel
14.59.18.Move the first row to the end of the table
14.59.19.Move the first two rows to the end of the table
14.59.20.Move the last two rows to the start of the table
14.59.21.Move the last row to the beginning of the table
14.59.22.Get all the table data from DefaultTableModel
14.59.23.Copy (clone) the data from the second row
14.59.24.Overwrite the date from the first row with DefaultTableModel
14.59.25.Add rows to a Table
14.59.26.Sharing a Table Model Between JTable Components
14.59.27.Disabling User Edits in a JTable with DefaultTableModel
14.59.28.Appending a Row to a JTable Component
14.59.29.Copy data from a table to a list
14.59.30.Map TableModel
14.59.31.Bean Property Table Model