Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I add  rows using an AbstractTableModel

Posted on 2000-02-25
5
Medium Priority
?
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

688 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