Class JKTableModel

java.lang.Object
javax.swing.table.AbstractTableModel
com.jk.core.model.table.JKTableModel
All Implemented Interfaces:
Serializable, TableModel
Direct Known Subclasses:
JKMapTableModel

public class JKTableModel extends AbstractTableModel
This class is a table model that extends AbstractTableModel to provide flexibility and control over table data and behavior.
Version:
1.0
Author:
Dr. Jalal H. Kiswani
See Also:
  • Constructor Details

    • JKTableModel

      public JKTableModel()
      This method constructs a new JKTableModel.
  • Method Details

    • addJKTableColumn

      public void addJKTableColumn(JKTableColumn col)
      This method adds a new column to the table model.
      Parameters:
      col - Specifies the new column.
    • addRecord

      public JKTableRecord addRecord()
      This method adds a new record to the table model.
      Returns:
      the newly added record.
    • addRecord

      public void addRecord(JKTableRecord record)
      This method adds a new record to the table model.
      Parameters:
      record - Specifies the new record.
    • clearRecords

      public void clearRecords()
      This method clears all the records contained within this table model.

      This table model will have no records after this method call.

    • createEmptyRecord

      protected JKTableRecord createEmptyRecord()
      This method creates a new JKTableRecord which is empty.
      Returns:
      the newly generated table record.
    • deleteRow

      public JKTableRecord deleteRow(int selectedRow)
      This method deletes an entire row from the table that is at the given index.
      Parameters:
      selectedRow - Specifies the selected row index.
      Returns:
      returns the row that got deleted.
    • deleteRows

      public void deleteRows(int[] rows)
      This method deletes the rows that are at the given indices.
      Parameters:
      rows - Specifies the rows that are to be removed indices.
    • fireTableColumnDataChanged

      public void fireTableColumnDataChanged(int col)
      This method notifies the listeners that all the values in the specified columns have been changed.
      Parameters:
      col - Specifies the column index whose cells have been changed.
    • getActualColumnCount

      public int getActualColumnCount()
      This method gets the number of actual columns.
      Returns:
      the number of actual columns.
    • getActualColumnIndexFromVisible

      public int getActualColumnIndexFromVisible(int visibleIndex)
      This method gets the actual column index from the visible index.
      Parameters:
      visibleIndex - Specifies the visible index.
      Returns:
      the actual column index.
    • getActualColumnName

      public String getActualColumnName(int index)
      This method gets the column name using the actual index.
      Parameters:
      index - Specifies the column index.
      Returns:
      the column name.
    • getCellEditor

      public TableCellEditor getCellEditor(int column)
      This method gets the cell editor using the column this cell is in.
      Parameters:
      column - Specifies the column the cell is in.
      Returns:
      the cell editor.
    • getCellRenderer

      public TableCellRenderer getCellRenderer(int column)
      This method gets the cell renderer using the column this cell is in.
      Parameters:
      column - Specifies the column the cell is in.
      Returns:
      the cell renderer.
    • getColumnClass

      public Class getColumnClass(int columnIndex)
      This method gets the column class whose index is specified.
      Specified by:
      getColumnClass in interface TableModel
      Overrides:
      getColumnClass in class AbstractTableModel
      Parameters:
      columnIndex - Specifies the column index.
      Returns:
      the column class.
    • getColumnCount

      public int getColumnCount()
      Returns the number of columns in the model. A JTable uses this method to determine how many columns it should create and display by default.
      Returns:
      the number of columns in the model
    • getColumnName

      public String getColumnName(int visibleColumnIndex)
      This method gets the column name using the given visible column index.
      Specified by:
      getColumnName in interface TableModel
      Overrides:
      getColumnName in class AbstractTableModel
      Parameters:
      visibleColumnIndex - Specifies the visible column index.
      Returns:
      the column name.
    • getColumnType

      public int getColumnType(int col)
      This method gets the column type to the column whose index is provided.
      Parameters:
      col - Specifies the column index.
      Returns:
      the column type whose index is provided.
    • getColunmIndex

      public int getColunmIndex(String name)
      This method gets the column index using its name.
      Parameters:
      name - Specifies the column name.
      Returns:
      the column index.
    • getColunmIndexByName

      public int getColunmIndexByName(String colName)
      This method gets the column index by the column name.
      Parameters:
      colName - Specifies the column name.
      Returns:
      the column index.
    • getColunmSum

      public double getColunmSum(int col)
      This method sums all the values inside the column whose index is provided.
      Parameters:
      col - Specifies the column index whose values are to be summed.
      Returns:
      the sum of the values inside the specified column.
    • getDeletedRecords

      public Vector<JKTableRecord> getDeletedRecords()
      This method gets the deleted records.
      Returns:
      the deleted records.
    • getDeletedRecordsAsDataVector

      public Vector<Vector> getDeletedRecordsAsDataVector()
      This method gets the deleted records.
      Returns:
      the deleted records.
    • getFormatter

      public Format getFormatter(int col)
      This method gets the formatter associated with the specified column.
      Parameters:
      col - Specifies the column index.
      Returns:
      the formatter.
    • getIntegerColunmSum

      public int getIntegerColunmSum(int col)
      This method sums all the values inside the column whose index is provided.
      Parameters:
      col - Specifies the column index whose values are to be summed.
      Returns:
      the sum of the values inside the specified column.
    • getPrefferedWidth

      public int getPrefferedWidth(int column)
      This method gets the preferred width of the specified column.
      Parameters:
      column - Specifies the column index.
      Returns:
      the preferred width
    • getRecord

      public JKTableRecord getRecord(int row)
      This method gets the record associated with the given index.
      Parameters:
      row - Specifies the record index.
      Returns:
      the record.
    • getRecords

      public Vector<JKTableRecord> getRecords()
      This method gets all the records.
      Returns:
      the records.
    • getRecordsAsDataVector

      public Vector<Vector> getRecordsAsDataVector()
      This method gets all the records.
      Returns:
      the records.
    • getRowCount

      public int getRowCount()
      Returns the number of rows in the model. A JTable uses this method to determine how many rows it should display. This method should be quick, as it is called frequently during rendering.
      Returns:
      the number of rows in the model
    • getTableColumn

      public JKTableColumn getTableColumn(int visibleColumnIndex)
      This method gets the table column associated with the provided visible index.
      Parameters:
      visibleColumnIndex - Specifies the visible column index.
      Returns:
      the table column.
    • getTableColumn

      public JKTableColumn getTableColumn(int col, boolean visibleIndex)
      This method gets the table column using the provided index.

      if the provided boolean value is true, the provided index is visible index, if its false, the provided index is an actual index.

      Parameters:
      col - Specifies the column index.
      visibleIndex - Indicated whether the index is a visible index or an actual index.
      Returns:
      the table column.
    • getValueAt

      public Object getValueAt(int row, int visibleColumnIndex)
      Returns the value for the cell at columnIndex and rowIndex.
      Parameters:
      row - the row whose value is to be queried
      visibleColumnIndex - the column whose value is to be queried
      Returns:
      the value Object at the specified cell
    • getValueAtAsDouble

      public double getValueAtAsDouble(int row, int col)
      This method gets the value at the cell with the provided coordinate as a double.
      Parameters:
      row - Specifies the row the cell is at
      col - Specifies the visible index of the column the cell is in.
      Returns:
      the value at the specified cell as double.
    • getValueAtAsFloat

      public float getValueAtAsFloat(int row, int col)
      This method gets the value at the cell with the provided coordinate as a float.
      Parameters:
      row - Specifies the row the cell is at
      col - Specifies the visible index of the column the cell is in.
      Returns:
      the value at the specified cell as float.
    • getValueAtAsInteger

      public int getValueAtAsInteger(int row, int col)
      This method gets the value at the cell with the provided coordinate as a integer.
      Parameters:
      row - Specifies the row the cell is at
      col - Specifies the visible index of the column the cell is in.
      Returns:
      the value at the specified cell as integer.
    • getVisibleColumnIndexFromActual

      public int getVisibleColumnIndexFromActual(int actualIndex)
      This method gets the visible column index from the provided actual index.
      Parameters:
      actualIndex - Specifies the column actual index.
      Returns:
      the visible column index.
    • insertRecord

      public void insertRecord(int selectedRow)
      This method insert an empty record at the specified row index.
      Parameters:
      selectedRow - Specifies the new record index.
    • insertRecord

      public void insertRecord(int row, JKTableRecord record)
      This method insert the specified record at the provided row index.
      Parameters:
      row - Specifies the new record index.
      record - Specifies the new record to be added.
    • isAllDataValid

      public boolean isAllDataValid()
      This method checks that the column values do not contain null values.
      Returns:
      true, if all data are valid, false otherwise.
    • isAllowDelete

      public boolean isAllowDelete()
      This method checks if this table allow deletion of data.
      Returns:
      true, if this table allow cell deletion and is editable, false otherwise.
    • isDataModified

      public boolean isDataModified()
      This method checks if the data have been modified.
      Returns:
      true, if the data have been modified, false otherwise.
    • isEditable

      public boolean isEditable()
      This method checks if this table is editable.

      This method will return true even if only one column is editable.

      Returns:
      true, if it is editable, false otherwise.
    • isEditable

      public boolean isEditable(int column)
      This method checks that the column at the provided index is editable.
      Parameters:
      column - Specifies the column index.
      Returns:
      true, if it is editable, false otherwise.
    • isEditable

      public boolean isEditable(int row, int column)
      This method checks if the cell at the provided coordinate is editable.
      Parameters:
      row - Specifies the row the cell is at.
      column - Specifies the column the cell is in.
      Returns:
      true, if it is editable, false otherwise.
    • isNumericClumn

      public boolean isNumericClumn(int visibleColIndex)
      This method checks whether this column contains numeric value or not.
      Parameters:
      visibleColIndex - Specifies the visible column index.
      Returns:
      true, if it contains numeric values, false otherwise.
    • isValidTableColumnIndex

      protected boolean isValidTableColumnIndex(int actualIndex)
      This method checks the provided index if it belongs to a valid column or not.
      Parameters:
      actualIndex - Specifies the actual column index.
      Returns:
      true, if it is valid column index, false otherwise.
    • isVisible

      public boolean isVisible(int col)
      This method checks whether the column associated with the provided index is visible or not.
      Parameters:
      col - Specifies the column index.
      Returns:
      true, if it is visible, false otherwise.
    • refreshVisibility

      public void refreshVisibility()
      This method re-index all the visible columns indices.
    • removeRecord

      public JKTableRecord removeRecord(int row)
      This method remove the record associated with the provided index.
      Parameters:
      row - Specifies the row index.
      Returns:
      the removed record.
    • resetRecords

      public void resetRecords()
      This method clears all the records inside the table.
    • setAllowDelete

      public void setAllowDelete(boolean allowDelete)
      This method specify whether this table should allow cell deletion or not.
      Parameters:
      allowDelete - Indicates the new allow deletion value.
    • setColumnValue

      public void setColumnValue(int row, int col, Object value, boolean visibleIndex)
      This method changes the value of the cell at the provided coordinate.

      if the provided column index is a visible index not an actual one, it will get the actual index associated with the visible index and use it instead.

      Parameters:
      row - Specifies the row the cell is at.
      col - Specifies the column the cell is in.
      value - Specifies the cell value.
      visibleIndex - Indicates whether the provided column index is a visible index or an actual index.
    • setEditable

      public void setEditable(boolean editable)
      This method specify whether this table is editable or not.
      Parameters:
      editable - Indicates the new editable value.
    • setEditable

      public void setEditable(int column, boolean editable)
      This method specify whether the provided column is editable or not.
      Parameters:
      column - Specifies the column index.
      editable - Indicates the new editable value of the column.
    • setEditable

      public void setEditable(int row, int col, boolean enable)
      This method specify whether the cell at the provided coordinate is editable or not.
      Parameters:
      row - Specifies the row the cell is at.
      col - Specifies the column the cell is in.
      enable - Indicates the new editable value of the cell.
    • setEditor

      public void setEditor(int colunm, TableCellEditor cellEditor)
      This method sets the column editor to the provided editor.
      Parameters:
      colunm - Specifies the column index.
      cellEditor - Specifies the new column editor.
    • setFormatter

      public void setFormatter(int col, Format formatter)
      This method sets the column formatter to the provided formatter.
      Parameters:
      col - Specifies the column index.
      formatter - Specifies the new column formatter.
    • setPreferredWidth

      public void setPreferredWidth(int col, int width)
      This method sets the column preferred width.
      Parameters:
      col - Specifies the column index
      width - Specifies the new width.
    • setRenderer

      public void setRenderer(int col, TableCellRenderer cellRenderer)
      This method sets the column renderer to the provided renderer.
      Parameters:
      col - Specifies the column index.
      cellRenderer - Specifies the new column renderer.
    • setRequired

      public void setRequired(int col, boolean required)
      This method specify whether the provided column is required or not.
      Parameters:
      col - Specifies the column index.
      required - Indicates the new required value of the column.
    • setValueAt

      public void setValueAt(Object value, int rowIndex, int visibleIndex)
      Sets the value in the cell at specified coordinate to the provided value.
      Specified by:
      setValueAt in interface TableModel
      Overrides:
      setValueAt in class AbstractTableModel
      Parameters:
      value - Specifies the new value.
      rowIndex - Specifies the row index where the cell is at.
      visibleIndex - Specifies the column visible index where the cell is in.
    • setVisible

      public void setVisible(int col, boolean visible)
      This method specify whether the provided column is visible or not.
      Parameters:
      col - Specifies the column visible index.
      visible - Indicates the new visible value of the column.
    • setVisibleByActualIndex

      public void setVisibleByActualIndex(int colunmIndex, boolean visible)
      This method specify whether the provided column is visible or not.
      Parameters:
      colunmIndex - Specifies the column actual index.
      visible - Indicates the new visible value of the column.
    • addJKTableColumn

      public void addJKTableColumn(String keyLabel)
      This method adds a new column to the table with the specified header name.
      Parameters:
      keyLabel - Specifies the new column name.
    • getTableColumns

      public Vector<JKTableColumn> getTableColumns()
      This method returns all the table columns model.
      Returns:
      table columns model.