?
Solved

Problem implementing the abstractTableModel

Posted on 2003-03-10
3
Medium Priority
?
451 Views
Last Modified: 2011-08-18
Hi:

    I have constructed a JTable using the abstractTableModel but I am having problem implementing some of the mehods of the AbstractTableModel such as setValueAt and getValueAt. Also I need to create a method to insert a row.

Here is a snippet of my code:

public class DataFileTableModel extends AbstractTableModel {
 protected Vector data;
 //protected Vector columnNames ;  
 protected String datafile;
   
 private Vector tableOfVectors = new Vector();  
 private Vector columnNames;
 Vector data_vector = new Vector();

 public DataFileTableModel(String f){
   datafile = f;
   initVectors();  
   }

 public void initVectors() {
     String aLine;
     //tableOfVectors = new Vector();
     columnNames = new Vector();
   try{
       Process ls_proc = Runtime.getRuntime().exec(datafile);
       BufferedReader br = new BufferedReader(new InputStreamReader( ls_proc.getInputStream()));  
        //extract coumn names
       StringTokenizer st1 =
           new StringTokenizer(br.readLine(), "|");
          while(st1.hasMoreTokens())
              columnNames.addElement(st1.nextToken());
            //extract data
           while ((aLine = br.readLine()) != null) {  
             StringTokenizer st2 = new StringTokenizer(aLine, "|");
                while(st2.hasMoreTokens())
                {
                    tableOfVectors.add(st2.nextToken() );
                }
     }
          br.close();  
   }
   catch (Exception e) {
      e.printStackTrace();
     }
 }

 public int getRowCount() {
   return tableOfVectors.size() / getColumnCount();
 
   }

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

 public String getColumnName(int columnIndex) {
   String colName = "";

   if (columnIndex <= getColumnCount())
      colName = (String)columnNames.elementAt(columnIndex);

   //return columnNames[columnIndex];
   return colName;
   }
 
 public Class getColumnClass(int columnIndex){
   return String.class;
   }
   
 public boolean isCellEditable(int rowIndex, int columnIndex) {
   return false;
   }
 
 public void insertRow(int index, Object[] element)
 {
     tableOfVectors.add(index,element);
     fireTableRowsInserted(0,0);
 }
 public void removeTask(int startRow,int endRow)
 {
      int tempRow = 0;
     int actualRows = 0;

    if (endRow < startRow)
    {
      tempRow = endRow ;
      endRow = startRow ;
      startRow = tempRow ; }

    if (startRow < 0 || endRow > getRowCount())
    return;

    actualRows = (endRow - startRow) + 1 ;

    // determine the starting point (cell) to start deleting at //
     int colCount = getColumnCount() ;  
     int cell = startRow * colCount ;

    // determine the total number of cells to delete //
    int totColCount = (getColumnCount() * actualRows) ;

    for (int d = 0; d < totColCount; d++)
    {
      tableOfVectors.remove(cell) ;
    }

    fireTableRowsDeleted(startRow,endRow) ;
   }

0
Comment
Question by:nutless
[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
  • 3
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 80 total points
ID: 8106147
Well you know what i'd say about that don't you? ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8106189
DefaultTableModel supplies the functionality of adding/deleting rows. If you extend DTM is there any reason why you would need to override set/getValueAt?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8106212
For that matter, is there any need to *extend* DTM as opposed to simply creating one?
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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.
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:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 18 hours left to enroll

762 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