【问题标题】:how to add a column to a jTable and populate on the fly如何将列添加到 jTable 并即时填充
【发布时间】:2015-08-18 10:36:03
【问题描述】:

我有一个 4 列 6 行的 Jtable 和一个 jButton。当单击 jButton 时,会运行一个查询并将结果存储在一个字符串变量中。在查询成功完成后,调用附加代码将列添加到现有 jTable。我的问题是如何使用来自 String 变量的数据填充新创建的列。我正在使用下面的代码

for(int col = 0; col<jTable2.getRowCount(); col++){ 
                 TableColumn c = new TableColumn();
                                    c.setHeaderValue("Test");
                                    ((DefaultTableModel) jTable2.getModel()).addColumn(c);
                                    jTable2.setValueAt("100", col, 12);
                 }

这可行,但问题是代码还添加了与 forloop 中的设置条件等效的附加列。我如何让它只添加一列并填充与 forloop 中设置的条件一样多的内容,而无需添加多列。我知道我的问题来自 forloop,但我不知道如何解决。

洋葱可以帮忙

【问题讨论】:

  • @KDMthanx 问题解决了
  • 删除评论并将其添加为答案。

标签: java swing for-loop jtable


【解决方案1】:

在 for 循环之外添加列,并在循环中为每一行设置值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 2013-06-15
    • 1970-01-01
    • 2019-07-04
    • 2011-07-10
    相关资源
    最近更新 更多