Creating simple JTable using AbstractTableModel : Table Model « Swing JFC « Java






Creating simple JTable using AbstractTableModel

    
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;
  }
}

   
    
    
    
  








Related examples in the same category

1.Append a row to a table through DefaultTableModel at specified row
2.Remove the first row from a table with DefaultTableModel
3.Remove the last row from a table with DefaultTableModel
4.Move the first row to the end of the table
5.Move the last row to the beginning of the table
6.Move the first two rows to the end of the table
7.Move the last two rows to the start of the table
8.Get all the table data from DefaultTableModel
9.Copy (clone) the data from the second row
10.Overwrite the date from the first row with DefaultTableModel
11.Copy data from a table to a list
12.Insert a new column to a table
13.JTable class using default table models and a convenienceJTable class using default table models and a convenience
14.extends DefaultTableModel to create your own table model and build table from thatextends DefaultTableModel to create your own table model and build table from that
15.Table with a custom TableModelTable with a custom TableModel
16.extends AbstractTableModel to create custom modelextends AbstractTableModel to create custom model
17.Table model is based on call backTable model is based on call back
18.Hard code data in array for TableModelHard code data in array for TableModel
19.A JTable class using default table models and a convenience constructorA JTable class using default table models and a convenience constructor
20.Custom model, POJO and JTableCustom model, POJO and JTable
21.Custom table model, File data based ModelCustom table model, File data based Model
22.Paging or pagable JTable(Table) Model for large data setPaging or pagable JTable(Table) Model for large data set
23.Paging JTable(Table) Model with an input field for dynamically altering the size of a page.Paging JTable(Table) Model with an input field for dynamically altering the size of a page.
24.File data Table: file name, size, type File data Table: file name, size, type
25.Stocks data Table: illustrate the TableModel Stocks data Table: illustrate the TableModel
26.AbstractTableModel backed by HashtableAbstractTableModel backed by Hashtable
27.Fixed data vs dynamic data TableFixed data vs dynamic data Table
28.Use model to control the Editable ColumnsUse model to control the Editable Columns
29.Converts a visible column index to a column index in the model.
30.Converts a column index in the model to a visible column index
31.Returns the visible columns in the order that they appear in the model
32.Appending a Column to a JTable Component using DefaultTableModel
33.Add a column with values.
34.Disable autoCreateColumnsFromModel
35.Add a column without affecting existing columns
36.Remove the first visible column without removing the underlying data
37.Sharing a Table Model Between JTable Components
38.Map TableModel
39.TableSorter extends AbstractTableModel
40.TableSorter is a decorator for TableModels
41.Bean Property Table Model
42.A simple extension of JTable that supports the use of a SortableTableModel.
43.This program shows how to build a table from a table modelThis program shows how to build a table from a table model