1

This is my TableModel, I have extended AbstractTableModel

class CustomTableModel extends AbstractTableModel
{
  String[] columnNames = {"Name","Contact","eMail","Address","City","Pin","State","Type","ID"};
  Vector<String[]> data = new Vector<String[]>();

  CustomTableModel()
  {
    try
    {
      //Using JDBC connection//
      while(rs.next())
      {
        String[] s=new String[9];
        s[0]=rs.getString(1);
        //System.out.println(s[0]);
        s[1]=rs.getString(2);
        s[2]=rs.getString(3);
        s[3]=rs.getString(4);
        s[4]=rs.getString(5);
        s[5]=rs.getString(6);
        s[6]=rs.getString(7);
        s[7]=rs.getString(8);
        s[8]=rs.getString(9);
        data.add(s);
      }
    }
    catch(Exception e)
    {
      System.out.println("the exception is :"+e.toString());
    }

  }
  public int getColumnCount() {
    int columnCount = columnNames.length;
    return columnCount;
  }
  public int getRowCount() {
    int rowCount = data.size();
    return rowCount;
  }
  public Object getValueAt(int rowIndex, int columnIndex) {
    return data.get(rowIndex)[columnIndex];
  }
  public String getColumnName(int column) {
    return columnNames[column];
  }
  public void removeRow(int r)
  {
    for(int i=0;i<data.size();i++)
    {
      String[] s = (String[])data.get(i);
      if(s[0]==getValueAt(r,0))
      {
        try
        {
          //using JDBC connections to delete the data from DB//
          //also removing the value from data and also updating the view//
          data.remove(data.get(i));
          fireTableRowsDeleted(r, r);
        }
        catch (Exception e)
        {
          System.out.println(e.toString());
        }
        break;
      }

    }
  }
  //I am using the following code to update the view but it doesnot work//
  public void addRow(String[] a)
  {
    data.add(a);
    fireTableRowsInserted(data.size() - 1, data.size() - 1);        
  }
}

I have a table class which extends CustomTableModel .

class table extends CustomTableModel
{
  final JButton editButton = new JButton("Edit");
  final JButton deleteButton = new JButton("Delete");
  final JTable mytable = new JTable(new CustomTableModel());
  . 
  .
  .
}      

I have a add button , and in its action listener i use the following code to pass the values that i wanted to add.

String[] a = {"a","b","c","d","e","f","g","h","i"};
table myTableObj = new table();
myTableObj.addRow(a);

Pls let me know where i am going wrong . Thanks

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
user2380811
  • 55
  • 1
  • 7
  • Have you used a debugger to prove that your `data.add(s);` line is being executed? – Duncan Jones May 22 '13 at 11:54
  • `I have extended AbstractTableModel` - Whats wrong with the DefaultTableModel? Your code is basically trying to duplicate that class. – camickr May 22 '13 at 21:40

3 Answers3

1

Pls let me know where i am going wrong . Thanks

String[] a = {"a","b","c","d","e","f","g","h","i"};
table myTableObj = new table();
myTableObj.addRow(a);
  • code lines talking about

    1. create a new row

    2. create a new JTable

    3. row is added to a new JTable

    4. result is that a new JTable is never added to visible Swing GUI

    5. don't do that, why is a new JTable recreated on every JButtons event

    6. add String[] a... to the CustomTableModel directly

  • for better help sooner post an SSCCE, short, runnable, compilable

mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

The table class makes no sense. It is supposed to be a TableModel that shoud be set into a JTable. Instead you have JTable declared as a field inside this table class (which should be Table btw according to Java naming convention). The result is that when constructing a new table object, a JTable is constructed inside it with another CustomTableModel inside. So the tableModel you are adding rows into is not the tableModel actually used by your JTable.

makasprzak
  • 5,082
  • 3
  • 30
  • 49
0

You can also use the myCustomTable.fireTableStructureChanged();

Sunny Patel
  • 535
  • 2
  • 5
  • 13