How to subclass DefaultTableModel and change its dataVector

I want to extend the DefaultTableModel and change its dataVector. I want to make the dataVector to show only specific fields of DataHolder in the column:

 public class MyTableModel extends DefaultTableModel {

     * The data vector
    private Vector<DataHolder>   dataVector_;

    //overridden method to add row in the table model
    public void addRow(DataHolder rowData) {
        insertRow(getRowCount(), rowData);

    public void insertRow(int row, DataHolder rowData) {
            dataVector_.insertElementAt(rowData, row);
            fireTableRowsInserted(row, row);

    ...} //end of MyTableModel

   class DataHolder{
     private int age;
     private int year;
     private int month;
     private int day;

How can i display specific DataHolder fields in my jtable? My table has 3 columns for month, day, and year.


You have to override the getValueAt(...) method:

public Object getValueAt(int row, int column) {
    DataHolder data = dataVector_.get(row);
    switch(column) {
        case 0: return data.month;
        case 1: return;
        case 2: return data.year;
        default: return null;

You need to create a custom model for this. The DefaultTableModel is not the best place to start.

Generally you would extend AbstractTableModel and use an ArrayList to store your DataHolder Objects. Then you need to implement the other methods of the TableModel interface. The Swing tutorial on How to Use Tables shows you the basics of how to do this.

Or you can use the Bean Table Model which does all the work for you.

Need Your Help

Closing a stream after an exception

c# .net stream serialization

I open a stream and then deserialize the data in the stream. However, I added a member to the SavedEventSet object so now when I try to open an old file, it throws the exception on the deserializa...

Rock, Paper, Scissors in MATLAB

matlab if-statement for-loop conditional conditional-statements

Alrighty, it's that time of week again where I am officially throwing my hands up in the air with MATLAB and asking for help. My goal this week is to try and make a function that takes in two inputs,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.