How could I empty my JTable (AbstractTableModel)? If I run tableModel.deleteData(); , then the message "0 >= 0" appears. Thanks!

class QueryTableModel extends AbstractTableModel {
  Vector cache; // will hold String[] objects
  int colCount;
  String[] headers;
  String url; String databasename;
  String login; String pass;
  Connection db;
  static Statement statement;

  public QueryTableModel(String url, String databasename, String login, String pass) {
    cache = new Vector();
    initDB(url, databasename, login, pass);
  }

  public static Statement getStatement() {
      return statement;
  }

  @Override
  public String getColumnName(int i) {
    return headers[i];
  }

  public int getColumnCount() {
    return colCount;
  }

  @Override
  public Class getColumnClass(int col) {
    return getValueAt(0, col).getClass();
  }

  @Override
  public int getRowCount() {
    return cache.size();
  }

@Override
  public Object getValueAt(int row, int col) {
    Object str = ((Object[]) cache.elementAt(row))[col];
    if (str != null)
        return str;
    else return "";
  }

  public void deleteData() {
    cache.clear();
    fireTableChanged(null);
  }
...
}

from my resources

public void deleteData() {
        int rows = getRowCount();
        if (rows == 0) {
            return;
        }
        cache.clear();
        fireTableRowsDeleted(0, rows - 1);
    }

Thank you a lot! It works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.