Link to home
Start Free TrialLog in
Avatar of nutless
nutless

asked on

Problem implementing the abstractTableModel

Hi:

    I have constructed a JTable using the abstractTableModel but I am having problem implementing some of the mehods of the AbstractTableModel such as setValueAt and getValueAt. Also I need to create a method to insert a row.

Here is a snippet of my code:

public class DataFileTableModel extends AbstractTableModel {
 protected Vector data;
 //protected Vector columnNames ;  
 protected String datafile;
   
 private Vector tableOfVectors = new Vector();  
 private Vector columnNames;
 Vector data_vector = new Vector();

 public DataFileTableModel(String f){
   datafile = f;
   initVectors();  
   }

 public void initVectors() {
     String aLine;
     //tableOfVectors = new Vector();
     columnNames = new Vector();
   try{
       Process ls_proc = Runtime.getRuntime().exec(datafile);
       BufferedReader br = new BufferedReader(new InputStreamReader( ls_proc.getInputStream()));  
        //extract coumn names
       StringTokenizer st1 =
           new StringTokenizer(br.readLine(), "|");
          while(st1.hasMoreTokens())
              columnNames.addElement(st1.nextToken());
            //extract data
           while ((aLine = br.readLine()) != null) {  
             StringTokenizer st2 = new StringTokenizer(aLine, "|");
                while(st2.hasMoreTokens())
                {
                    tableOfVectors.add(st2.nextToken() );
                }
     }
          br.close();  
   }
   catch (Exception e) {
      e.printStackTrace();
     }
 }

 public int getRowCount() {
   return tableOfVectors.size() / getColumnCount();
 
   }

 public int getColumnCount(){
   return columnNames.size();
   }

 public String getColumnName(int columnIndex) {
   String colName = "";

   if (columnIndex <= getColumnCount())
      colName = (String)columnNames.elementAt(columnIndex);

   //return columnNames[columnIndex];
   return colName;
   }
 
 public Class getColumnClass(int columnIndex){
   return String.class;
   }
   
 public boolean isCellEditable(int rowIndex, int columnIndex) {
   return false;
   }
 
 public void insertRow(int index, Object[] element)
 {
     tableOfVectors.add(index,element);
     fireTableRowsInserted(0,0);
 }
 public void removeTask(int startRow,int endRow)
 {
      int tempRow = 0;
     int actualRows = 0;

    if (endRow < startRow)
    {
      tempRow = endRow ;
      endRow = startRow ;
      startRow = tempRow ; }

    if (startRow < 0 || endRow > getRowCount())
    return;

    actualRows = (endRow - startRow) + 1 ;

    // determine the starting point (cell) to start deleting at //
     int colCount = getColumnCount() ;  
     int cell = startRow * colCount ;

    // determine the total number of cells to delete //
    int totColCount = (getColumnCount() * actualRows) ;

    for (int d = 0; d < totColCount; d++)
    {
      tableOfVectors.remove(cell) ;
    }

    fireTableRowsDeleted(startRow,endRow) ;
   }

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
DefaultTableModel supplies the functionality of adding/deleting rows. If you extend DTM is there any reason why you would need to override set/getValueAt?
For that matter, is there any need to *extend* DTM as opposed to simply creating one?