jtable add row

J

Jacques Chaurette

Hi all and thanks for any help. I have a jtable in whch I wish to add a row,
the constuctor is like this:

class MyTableModel extends AbstractTableModel {

String[] columnNames = {"Nom. dia.","Dia. (in)",
"Instal. Cost",
};

Object [][] data = pipeinitialvalue;


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

public int getRowCount() {
return data.length;
}

public void addRow(Object[] row) {

for (i=0; i<3; i++) {
data[data.length+1] = row;
}
fireTableRowsUpdated(data.length+1,data.length+1);

}

public String getColumnName(int col) {
return columnNames[col];
}

public Object getValueAt(int row, int col) {
return data[row][col];
}

/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
//if (col < 2) {
//return false;
//} else {
return true;
//}
}

/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}

data[row][col] = value;
fireTableCellUpdated(row, col);

if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}

private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();

for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}


when I try to add a row with jTable1.addRow(testRow);
the compiler throws an exception: ..cannot resolve symbol method addRow

Jacques
 
R

Roedy Green

Hi all and thanks for any help. I have a jtable in whch I wish to add a row,
the constuctor is like this:

class MyTableModel extends AbstractTableModel {
Object [][] data = pipeinitialvalue;

you would have to create a new array Object[][] with one more row, and
copy the old rows over one by one. You don't need to allocate new
rows, just copy over a reference to each row. e.g.

n++;
Object[][] datanew;
// allocate pointers to rows
datanew = new Object[n][];
for ( i=0; i<n-1; i++)
{
// copy reference to row
datanew = dataold;
}
// allocate new row
datanew [n-1] = new Object[100];
// drop reference to old array
dataold = null;

If you are adding a row often, use an Arraylist<Object[]> instead. It
will grow the array automatically and give itself some growing room
each time.
 
Z

zero

Hi all and thanks for any help. I have a jtable in whch I wish to add
a row, the constuctor is like this:

when I try to add a row with jTable1.addRow(testRow);
the compiler throws an exception: ..cannot resolve symbol method
addRow

Jacques

Like I said in my reply to your other post, extend DefaultTableModel
instead of AbstractTableModel. It will make your life much easier.

As for your error (NOT an exception, an exception is a run-time error-
trapping mechanism, not a compile-time error): AbstractTableModel and
TableModel don't have an addRow method, so you would have to write your
own.

But again, try extending DefaultTableModel. This already has an addRow
method, which will be much more efficient than adding data to an Object
array. Refer to my previous post for details.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top