Extending AbstractTableModel : JTable Model « Swing « Java Tutorial






Extending AbstractTableModel
import java.awt.BorderLayout;
import java.awt.Container;
import java.util.Calendar;
import java.util.GregorianCalendar;

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

class TableValues extends AbstractTableModel {

  public final static int FIRST_NAME = 0;

  public final static int LAST_NAME = 1;

  public final static int DATE_OF_BIRTH = 2;

  public final static int ACCOUNT_BALANCE = 3;

  public final static int GENDER = 4;

  public final static boolean GENDER_MALE = true;

  public final static boolean GENDER_FEMALE = false;

  public Object[][] values = {
      { "C", "A", new GregorianCalendar(1962, Calendar.FEBRUARY, 20).getTime(),
          new Float(15.67), new Boolean(GENDER_MALE) },
      { "J", "A", new GregorianCalendar(1987, Calendar.JANUARY, 6).getTime(),
          new Float(2.78), new Boolean(GENDER_MALE) },
      { "J", "A", new GregorianCalendar(1989, Calendar.AUGUST, 31).getTime(),
          new Float(3.89), new Boolean(GENDER_FEMALE) },
      { "E", "K", new GregorianCalendar(1945, Calendar.JANUARY, 16).getTime(),
          new Float(-4.70), new Boolean(GENDER_FEMALE) },
      { "B", "S", new GregorianCalendar(1907, Calendar.AUGUST, 2).getTime(),
          new Float(5.00), new Boolean(GENDER_FEMALE) } };

  public int getRowCount() {
    return values.length;
  }

  public int getColumnCount() {
    return values[0].length;
  }

  public Object getValueAt(int row, int column) {
    return values[row][column];
  }

}

public class ExtendsAbstractTableModel extends JFrame {

  protected JTable table;

  public static void main(String[] args) {
    ExtendsAbstractTableModel stt = new ExtendsAbstractTableModel();
    stt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    stt.setSize(400, 200);
    stt.setVisible(true);
  }

  public ExtendsAbstractTableModel() {
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    TableValues tv = new TableValues();
    table = new JTable(tv);
    pane.add(table, BorderLayout.CENTER);
  }

}








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