Problem adding a row to a table

Hi:


I am having problem adding a row to my table.
I am using the abstractTableModel.
Here snippet of it :

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 Object getValueAt(int rowIndex, int columnIndex) {
     return (String)tableOfVectors.elementAt( (rowIndex * getColumnCount()) + columnIndex);  
    }
   
  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
     tableOfVectors.setElementAt(aValue,(rowIndex * getColumnCount()) + columnIndex );
      fireTableCellUpdated(rowIndex,columnIndex);
   
    }
  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) ;
    }
 
I have a Jdialog in which the user enter the required information.
This is passed to the abstractTableModel(model_pass) like this:
 if (flags == 1)
                                                {  
                                                 model_pass.insertRow(0,new Object[]{ Name,"local",Interface,Description,"Up","0","No"});
                                                   
                                                }

The problem is that I keep getting the "ClassCastException at the getValueAt() of the abstractTableModel".  Also  "fireTableCellUpdated of the setValueAt() is highlighted.

I can't figure out the problem.  

--nutless
nutlessAsked:
Who is Participating?
 
functionpointerConnect With a Mentor Commented:
if this returns 'Object'
> public Object getValueAt(int rowIndex, int columnIndex) {
>    return (String)tableOfVectors.elementAt( (rowIndex * getColumnCount()) + columnIndex);  
>   }

why cast it before returning it?
0
 
membershCommented:
If  tableOfVectors is data matrix or simply put the vector that holds table data, then it should be vector of vectors or vector of object arrays, but should follow any one convention throughout.
But in
       while(st2.hasMoreTokens()){                    tableOfVectors.add(st2.nextToken() ); }

String is directly added to it. Instead, add it to temp vecotr tempVec, then add tempVec to tableOfVectors once u come out of while loop..

Also in insertRow, u r accepting object[], & adding it to tableOfVectors. Instead accept Vector..


0
 
CEHJCommented:
Why extend AbstractTableModel and not DefaultTableModel? The latter supports adding/deleting of rows.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
nutlessAuthor Commented:
Hi:

   I need to keep the way I am adding the string to the vector as it is. If I add the string to a vector then out of the while loop I add that vector to another vector the table does n't come out right.

Can someone show me how I can modify my setValueAt method and getValueAt method to get my program working.

--thanks nutless
0
 
CEHJCommented:
>>I need to keep the way I am adding the string to the vector as it is.

I'm just suggesting that you extend a different class that supports adding/deleting rows out of the box. You can keep the vecctor creation stuff.
0
 
nutlessAuthor Commented:
Originally I did it with DefaultTableModel then I ran into some problem near the end.

I can't keep changing back and forth.

I want to insert a row at the top of the table. But what I am getting is a row that is inserted into the cell of row one and column column.

0
All Courses

From novice to tech pro — start learning today.