Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTable woes with getting wrong index from rowSelected

843804Dec 14 2004 — edited Dec 16 2004
Okay, so I have these two buttons that add a row to a particular jtable, and delete a row. THe row is added to the end of the table, thats fine... the row can be delted from anywhere though.

The problem im having is reproducable, but the steps to reproduce it are general and dont happen in the same sequence all the time so I cannot figure whats going on.

Summary: Basically if I add and delete the rows enough ill eventually crash my program cause it tries to setValueAt from the tablemodel using a row index that doesnt exsist...and I cannot figure out how to fix this. I will post the code for:
my adding of the object (which adds the object to the table at the end of the array that is used for the table model)

Deleting of the object

The table models: setValueAt call and setData call.

One important note before posting the code: first of all im new to swing, jtables and the utility classe of java, so if did something really stupid you notice thats merely due to lack of experience with all the things java and swing can do. Secondly: my setData takes (Object[] objData) and sets the table model to this, this is an array of an objet i call Clauses....the table im using loads clasues from a database (which is just an object holding various "fields" of interest for a particular app)...the jtable in question can then add or delete clauses. thats where the problem lies. Without further ado, the code:
  public void m_jbtnDeleteClause_actionPerformed(ActionEvent e) {
      /**
       * @todo this really needs to be fixed, some error causing it to crash thanks to an
       * array out of bounds error.
       */

    int rowSel = m_clTable.getSelectedRow();  //find the row they clicked on

    if(rowSel ==-1) //make sure its a valid row
       return;

    ArrayList anArrayList=new ArrayList(); //create a new array list
    Clause[] theClauses = null; //lets set up a new array of clasues
    theClauses=(Clause[])m_clTableModel.getAllData(); //get all the clauses in the table

    for(int i=0; i<theClauses.length; i++)
    {
      anArrayList.add(i, theClauses);
}

//get selected row, and then trash the rest.
anArrayList.remove(rowSel);

Clause[] newClause=new Clause[anArrayList.size()];
newClause = (Clause[]) anArrayList.toArray(newClause); //recopy the array with one less
m_clTableModel.setData(newClause);
}


public void m_jbtnAddClause_actionPerformed(ActionEvent e) {
//adding a row, get the dataobj
Clause[] theClauses = null;

theClauses=(Clause[])m_clTableModel.getAllData();

ArrayList anArrayList = new ArrayList(); //set up a new list

for(int i=0; i<theClauses.length; i++)
{
anArrayList.add(i, theClauses[i]); //copy the array of cluases to this array list
}



//Set up the blank clause to add
Clause newClause = new Clause();

Parameter defParam=new Parameter();
defParam.setDescription("newparam: desc");
defParam.setDomain("newparam: domain");
defParam.setName("newparam: name");
defParam.setType("newparam: type");
defParam.setUnit("newparam: unit");

newClause.setOperator(">");
newClause.setParameter(defParam);
newClause.setThreshold((float)0.0);


//add the blank clause to the array list
anArrayList.add(newClause);


Clause[] newClauseArray=new Clause[anArrayList.size()];
//copy array list back
newClauseArray = (Clause[]) anArrayList.toArray(newClauseArray);

//set the ttable model again , this takes (Object[] data); as an argument
m_clTableModel.setData(newClauseArray);
}


//from the model:
public void setData(Object[] objData)
{
//fill out the kind of object I want to fill out.
//Object[] m_objData = Clause[] objData;

this.m_objData = objData;
System.out.println("Size: " + m_objData.length);
fireTableDataChanged();
}

public void setValueAt(Object value, int row, int col)
{
Clause data = (Clause)m_objData[row]; //crashes here
...
...
...
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 13 2005
Added on Dec 14 2004
8 comments
475 views