Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I add  rows using an AbstractTableModel

Posted on 2000-02-25
5
Medium Priority
?
317 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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

824 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