将行插入 JTable 时出现重复值

发布于 2024-07-18 22:21:50 字数 384 浏览 2 评论 0原文

我在将行插入 JTable 时遇到问题,但我不知道问题是什么。

我确实这样做:

((DefaultTableModel)myJTable.getModel()).insertRow(0,webSiteDownloader.getWebSites().toArray());

webSiteDownloader 是一个具有 ArrayList 的对象。 我可以调用 getWebSites 方法来获取该数组。

问题是,当我插入一行并添加第二行时,JTable 仅显示第一行,但重复两次。 我说得够清楚吗?

谢谢:D

I have problems on inserting rows into a JTable and I don't know what the problem is.

I do exactly like this:

((DefaultTableModel)myJTable.getModel()).insertRow(0,webSiteDownloader.getWebSites().toArray());

The webSiteDownloader is one object that have an ArrayList. I can get that array calling the method getWebSites.

The problem is that when I insert one row, adding the second one, the JTable only shows the first one, but repeated twice. Was I clear enough?

Thks :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花落人离去 2024-07-25 22:21:50

我会在黑暗中尝试一下,猜测您想要完成这样的事情:

DefaultTableModel dtm = (DefaultTableModel)myJTable.getModel();
for (MyRowObject row : webSiteDownloader.getWebSites()) {
  dtm.insertRow(0, row.toArray());
}

您使用 insertRow 而不是 addRow 是否有特殊原因?

另外,我真的很想建议您通过扩展 AbstractTableModel 来推出自己的特殊用途 TableModel。 基本未经测试的示例:

public class MyTableModel extends AbstractTableModel
{
  protected List<MyObject> rows;

  public MyTableModel()
  {
    rows = new ArrayList<MyObject>();
  }

  public void add(MyObject obj)
  {
    rows.add(obj);
  }

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

  @Override
  public int getColumnCount()
  {
    // This value will be constant, but generally you'd also
    // want to override getColumnName to return column names
    // from an array, and in that case you can return the length
    // of the array with column names instead
    return 2;
  }

  @Override
  public Object getValueAt(int row, int column)
  {
    MyObject obj = rows.get(row);

    // Change this to match your columns
    switch(column) {
      case 0: return obj.getId();
      case 1: return obj.getName();
    }

    return null;
  }
}

I'll take a shot into the dark and guess that you want to accomplish something like this:

DefaultTableModel dtm = (DefaultTableModel)myJTable.getModel();
for (MyRowObject row : webSiteDownloader.getWebSites()) {
  dtm.insertRow(0, row.toArray());
}

Is there a special reason that you're using insertRow instead of addRow?

Also, I'd really like to recommend that you roll your own special purpose TableModel by extending AbstractTableModel. Basic untested example:

public class MyTableModel extends AbstractTableModel
{
  protected List<MyObject> rows;

  public MyTableModel()
  {
    rows = new ArrayList<MyObject>();
  }

  public void add(MyObject obj)
  {
    rows.add(obj);
  }

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

  @Override
  public int getColumnCount()
  {
    // This value will be constant, but generally you'd also
    // want to override getColumnName to return column names
    // from an array, and in that case you can return the length
    // of the array with column names instead
    return 2;
  }

  @Override
  public Object getValueAt(int row, int column)
  {
    MyObject obj = rows.get(row);

    // Change this to match your columns
    switch(column) {
      case 0: return obj.getId();
      case 1: return obj.getName();
    }

    return null;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文