• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

How can I add rows using an AbstractTableModel

Using Swing jdk1.2
How can I add a row like you would with DefaultTableModel when I am using the
AbstractTabeleModel
I have created aTwo Dimensional array to store the row and column data
but i need to add a few more rows after the table had been constructed
With the DefaultTableModel you can just create a one dim array for the row data and add it to the model.
Basically so I need my Two dim Array to expand
but i havent a clue where to go now
Any help greatly appreciated
Regards Dave


0
geronimo2
Asked:
geronimo2
1 Solution
 
kylarCommented:
Well, what you are describing is not possible. An Abstract table model can't add a row. but the table model that you are storing as an AbstractTableModel isn't really an AbstractTableModel, it's a concrete instantiation of another class that implements it. so you need to make sure that the concrete implementation has a method like addRow(Vector RowToAdd) then cast it like this:

public class tableManipulator{

public tableManipulator(){
DefaultTableModel myModel = new DefaultTableModel();

//assume there is data in the table model

doTableRowAdd(myModel);



}
public void doTableRowAdd(AbstractTableModel abstractModel){

//now here you are getting an 'Abstract' table model, but it's really
//a concrete class (DefaultTableModel). so what we do is something like this:

Vector newRowData = new Vector();
//assume we add data to this vector;

//now we cast the abstract to a default, since we know what it is really:

DefaultTableModel dtm = (DefaultTableModel)abstractModel;

//then we add to it
dtm.addRow(newRowData);

}

}
}

If that's not what you want, I'm sorry :) post your code and we'll see what we can do
0
 
fmaritatoCommented:
I would strongly suggest NOT using a two dimensional array to store your elements in your table model. This is very inefficient with respect to memory usage. Plus, when you add a row, you have to construct a new array and copy the items into it. A very expensive process. I would suggest using a hashtable and use a Point as the indexing object. I have attached a sample for you to look at. In this example, I used the setValueAt method as my addRow method. Let
me know if you have any further problems...


import javax.swing.table.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.*;

public class MyAbstractTableModel extends AbstractTableModel {

      private Vector    headings = new Vector();
      private Hashtable data     = new Hashtable();
      private int       numRows  = 0;
      private TableSorter sorter = null;
/**
 * MyAbstractTableModel constructor comment.
 */
public MyAbstractTableModel() {
      super();
}
/**
 *
 * @param heading java.lang.String
 */
public void addHeading(String heading) {
      headings.addElement(heading);
}
/**
 *
 */
public void clearTable() {
      numRows = 0;
      if (data == null) {
            data = new Hashtable();
      } else {
            data.clear();
      }
      super.fireTableDataChanged();
}
/**
 *
 * @param row int
 */
public void deleteRow(int row) {
      for (int i=0; i<headings.size(); i++) {
            Object o = data.remove(new Point(row, i));
      }
      numRows--;
      super.fireTableDataChanged();
}
/**
 * getColumnCount method comment.
 */
public int getColumnCount() {
      return headings.size();
}
/**
 *
 * @return java.lang.String
 * @param col int
 */
public String getColumnName(int col) {
      return (String) headings.elementAt(col);
}
/**
 * getRowCount method comment.
 */
public int getRowCount() {
      return numRows;
}
/**
 * getValueAt method comment.
 */
public Object getValueAt(int row, int col) {
      return data.get(new Point(row,col));
}
/**
 *
 * @param sorter ui.tool.TableSorter
 */
public void setSorter(TableSorter sorter) {
      this.sorter = sorter;
}
/**
 *
 * @param obj java.lang.Object
 * @param row int
 * @param col int
 */
public void setValueAt(Object obj, int row, int col) {
      data.put(new Point(row, col), obj);
      if (row >= numRows) {
            numRows++;
      }
      super.fireTableDataChanged();
}
}

0
 
diakovCommented:
Which means, don't use default table model but implement your own. It is easy and there lots of examples on the sun site.

  Nik
0
 
geronimo2Author Commented:
Note Sorry about delay in responding as I have Limited Internet access here in college.

:) fmaritato
Thanks fmaritato I sorted it out by using DefaultTableModel.
Initially I had implemented by own AbstractTableModel similiar to the one you had outlined above but I had problems with rendering the cells which I have now sorted out.
It was your suggestion about using Vectors etc which helped me.Thanks alot
Regards Dave.

:)kylar
Thanks for suggestion there is a seperate 50 points for you to claim also.
Sound lads Thanks alot for the help
Regards Dave



0
 
geronimo2Author Commented:
Note Sorry about delay in responding as I have Limited Internet access here in college.

:) fmaritato
Thanks fmaritato I sorted it out by using DefaultTableModel.
Initially I had implemented by own AbstractTableModel similiar to the one you had outlined above but I had problems with rendering the cells which I have now sorted out.
It was your suggestion about using Vectors etc which helped me.Thanks alot
Regards Dave.

:)kylar
Thanks for suggestion there is a seperate 50 points for you to claim also.
Sound lads Thanks alot for the help
Regards Dave



0

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now