Solved

How can I add  rows using an AbstractTableModel

Posted on 2000-02-25
5
287 Views
Last Modified: 2009-07-29
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
Comment
Question by:geronimo2
5 Comments
 
LVL 4

Expert Comment

by:kylar
ID: 2559724
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
 
LVL 1

Accepted Solution

by:
fmaritato earned 300 total points
ID: 2559749
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
 
LVL 8

Expert Comment

by:diakov
ID: 2563088
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
 

Author Comment

by:geronimo2
ID: 2579374
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
 

Author Comment

by:geronimo2
ID: 2579376
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Basic Java Case or If-Else statement... 3 50
nextBoolean(double p) for Random class 3 40
servlet example 17 32
CSV file parsing thru Java 13 31
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

773 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