Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with overwriting DefaultTableModel 1

Status
Not open for further replies.

frag

Programmer
Dec 7, 2000
321
GB
Hi!

I have tried to overwrite the DefaultTableModel (making my own model extending the default one) but without success. At first I ave used the default table model directly but now I have to use my own model.

I used to call the DefaultTableModel like this:

Code:
ttmCorrections		= new DefaultTableModel(vecCorrections, vecCorr_Headers);

Where vecCorrections was an empty vector(!). I wanted only to have the headers and add data later ... this worked fine. But now, after having overwritten the DefaultTableModel in my own model-class the table won't get build. I think I don't call the contructor in a right way or made a mistake in overwriting it:

Code:
import javax.swing.table.DefaultTableModel;
import java.util.*;

/**
 *
 *
 */

class CorrectionTableModel extends DefaultTableModel {

  private Vector headerVec;
  private Vector rowVec;
  private Vector colVec = new Vector();

  public CorrectionTableModel(Vector rVec, Vector hVec) {
    super(rVec, hVec);
    this.rowVec = rVec;
    this.headerVec = hVec;
  }


What would be the correct way to overwrite the constructor...?


Cheers

frag

patrick.metz@epost.de
 
Where do you want to store your data ? In the "DefaultTableModel" or in your own "CorrectionTableModel" ? Now you are storing them in both.
 
Hm... I want them to store in the CorrectionTableModel but I don't have to... I appreciate every solution as long as I have access to the data in my 'main' class.

I guess if I would store the data in the DefaultTableModel I just would have to change my 'getTableValues' method in the class CorrectionTableModel:

Code:
  public Vector getTableValues()
  {
   return rowVec;
  }

And the other methods that return data values (like getValueAt(), etc.).


frag

patrick.metz@epost.de
 
Thread269-614169 contains an example of a class that extends "AbstractTableModel". Then you have complete control over your data.
(PS: there is also an example in chapter6 of "Core swing advanced programming")
 
I can't use AbstractTableModel because I need the DefaultTableModel's method 'addRow()' and I don't know how to write them on my own.

The problem that started all this was that I wanted to have checkboxes for a row of boolean values in my table but whenever I put a 'new Boolen(flase)' in the row the cells just display "false" instead of the checkbox.

patrick.metz@epost.de
 
I can't test it for the moment but implement "getColumnClass(...)" and let it return Boolean for that column.
Code:
  public Class getColumnClass(int col) {
    switch (col) {
      case 0: return Boolean.class;
      case 1: return Date.class; 
      case 2: return String.class;
    }
  }
 
Aaah! I see... will try that. But there is still this problem with my constructor and (empty) vector... the table doesn't want to appeare on my JPanel... :-(

patrick.metz@epost.de
 
I can't belive it, this really works!!

I have deleted every method and the constructor in my CorrectionTableModel class except of the methods isCellEditable and getColumnClass and there is my table!

I think the was something wrong with one (or more) of the other methods (constructor, getColumnName, getValueAt, setValueAt, getRowCount, getColumnCount). I just would like to know what was wrong... anyway a star for hologram. Your example works (ok, haven't tried it with the switch but with an if-clause).

Thanx again!


Cheers

frag

patrick.metz@epost.de
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top