如何将整个jtable的行保存到另一个表中?
我试图验证当列步骤等于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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用setValueat(...)方法,因为第二个表中的行不存在。您需要将新的数据添加到表的模型中。
您可以通过:
jtable_step2.addrow(row)将新的数据添加到表的模型中。
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:
jtable_step2.addRow( row )
to add a new row of data to the model of the table.