如何将整个jtable的行保存到另一个表中?

发布于 2025-01-27 07:10:13 字数 1505 浏览 4 评论 0原文

我试图验证当列步骤等于2时,将行复制到另一个jtable

但是jtable_step2我尚未正确初始化, 这就是为什么它返回错误的原因:

jtable_step2.setValueAt(jtable.getValueAt(i, j), row, j);

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0

如何复制满足条件的相同列和行?

Java代码:

import javax.swing.JTable;

public class TestJTableCopy {

    public static void main(String args[]){
        String data[][]={ {"1","/LOCAL/USER/LOCAL", "20220421", "1"},
                          {"1","/LOACL/USER/LOCAL", "20220421", "2"},
                          {"1","/LOACL/USER/LOCAL", "20220422", "2"} };
        String columns[] = {"LINE", "SOURCE", "DATE", "STEP"};
        final JTable jtable = new JTable(data,columns);
        
        JTable jtable_step2 = new JTable();
        //jtable2.addColumn(columns);
        int row = 0;
        for (int i = 0; i < jtable.getRowCount(); i++) {
            //STEP == 2
            if (jtable.getValueAt(i, 3).equals("2")) {

                for(int j = 0; j < jtable.getColumnCount(); j++) {
                    jtable_step2.setValueAt(jtable.getValueAt(i, j), row, j);
                }
                row++;
            }
        }
        
        for (int i = 0; i < jtable_step2.getRowCount(); i++) {
            for (int j = 0; j < jtable_step2.getColumnCount(); j++) {
                System.out.println(i + " " + j + " " + jtable_step2.getValueAt(i, j));
            }
        }
   }
}

I am trying to validate that when the column step is equal to 2 the row is copied to another JTable.

But the jtable_step2 I have not initialized correctly,
that's why it returns the error:

jtable_step2.setValueAt(jtable.getValueAt(i, j), row, j);

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0

How do I copy the same columns and rows that satisfy the condition?

Java code:

import javax.swing.JTable;

public class TestJTableCopy {

    public static void main(String args[]){
        String data[][]={ {"1","/LOCAL/USER/LOCAL", "20220421", "1"},
                          {"1","/LOACL/USER/LOCAL", "20220421", "2"},
                          {"1","/LOACL/USER/LOCAL", "20220422", "2"} };
        String columns[] = {"LINE", "SOURCE", "DATE", "STEP"};
        final JTable jtable = new JTable(data,columns);
        
        JTable jtable_step2 = new JTable();
        //jtable2.addColumn(columns);
        int row = 0;
        for (int i = 0; i < jtable.getRowCount(); i++) {
            //STEP == 2
            if (jtable.getValueAt(i, 3).equals("2")) {

                for(int j = 0; j < jtable.getColumnCount(); j++) {
                    jtable_step2.setValueAt(jtable.getValueAt(i, j), row, j);
                }
                row++;
            }
        }
        
        for (int i = 0; i < jtable_step2.getRowCount(); i++) {
            for (int j = 0; j < jtable_step2.getColumnCount(); j++) {
                System.out.println(i + " " + j + " " + jtable_step2.getValueAt(i, j));
            }
        }
   }
}

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

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

发布评论

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

评论(1

£烟消云散 2025-02-03 07:10:13
jtable_step2.setValueAt(jtable.getValueAt(i, j), row, j);

您不能使用setValueat(...)方法,因为第二个表中的行不存在。您需要将新的数据添加到表的模型中。

您可以通过:

  1. 创建一个向量,可以说“行”
  2. 遍历要复制的行的所有列的所有列,然后将每个项目添加到
  3. 第二个表中的表中,您使用jtable_step2.addrow(row)将新的数据添加到表的模型中。
jtable_step2.setValueAt(jtable.getValueAt(i, j), row, j);

You can't use the setValueAt(...) method because the row doesn't exist in the second table. You need to add a new row of data to the model of the table.

You do this by:

  1. Creating a Vector, lets say "row"
  2. Iterate through all the columns of the row you want to copy and add each item to the table
  3. in the second table you use jtable_step2.addRow( row ) to add a new row of data to the model of the table.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文