Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Problem implementing the abstractTableModel

Posted on 2003-03-10
3
Medium Priority
?
458 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
  • 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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

581 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