1

这是我的 TableModel,我扩展了 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);        
  }
}

我有一个扩展 CustomTableModel 的表类。

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

我有一个添加按钮,在它的动作监听器中我使用下面的代码来传递我想要添加的值。

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

请让我知道我哪里出错了。谢谢

4

3 回答 3

1

请让我知道我哪里出错了。谢谢

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

    1. 创建一个新行

    2. 创建一个新的JTable

    3. 行被添加到一个新的JTable

    4. 结果是一个新JTable的永远不会添加到可见的 Swing GUI

    5. 不要那样做,为什么JTable在每个JButtons 事件上都重新创建一个新的

    6. 直接添加String[] a...CustomTableModel

  • 为了获得更好的帮助,请尽快发布SSCCE,简短,可运行,可编译

于 2013-05-22T12:00:44.560 回答
0

您还可以使用 myCustomTable.fireTableStructureChanged();

于 2013-05-22T16:14:38.037 回答
0

没有意义。它应该是一个应该设置为 JTable 的 TableModel。相反,您将 JTable 声明为该类中的一个字段(根据 Java 命名约定,它应该是Table btw)。结果是在构造一个新的对象时,在里面构造了一个JTable,里面还有另一个CustomTableModel。因此,您要向其中添加行的 tableModel 不是 JTable 实际使用的 tableModel。

于 2013-05-22T12:07:20.753 回答