Problem adding a row to a table

Posted on 2003-03-09
Medium Priority
Last Modified: 2008-02-01

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;

  public void initVectors() {
      String aLine;
      //tableOfVectors = new Vector();
      columnNames = new Vector();
        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(), "|");
             //extract data
            while ((aLine = br.readLine()) != null) {  
              StringTokenizer st2 = new StringTokenizer(aLine, "|");
                     tableOfVectors.add(st2.nextToken() );
    catch (Exception e) {

  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 );
  public void insertRow(int index, Object[] element)
  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())

     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.  

Question by:nutless
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

functionpointer earned 80 total points
ID: 8100721
if this returns 'Object'
> public Object getValueAt(int rowIndex, int columnIndex) {
>    return (String)tableOfVectors.elementAt( (rowIndex * getColumnCount()) + columnIndex);  
>   }

why cast it before returning it?

Expert Comment

ID: 8101878
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..

LVL 86

Expert Comment

ID: 8101943
Why extend AbstractTableModel and not DefaultTableModel? The latter supports adding/deleting of rows.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 8103872

   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
LVL 86

Expert Comment

ID: 8104348
>>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.

Author Comment

ID: 8104387
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.


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question