如何将表列值传输到一列表?

时间:2022-05-22 01:07:39

I have two tables, one with 4 columns and the other with one column, How could we copy the values the second column of the first table to the table with one column in Java. I tried this algorithm (tab1 is the one column table, tab2 is the 4 columns table)

我有两个表,一个有4列,另一个有一列,我们如何将第一个表的第二列的值复制到Java中有一列的表。我试过这个算法(tab1是一列表,tab2是4列表)

int i=0;
JTable tab1=null;
for ( i=1;i<colnumber;i++)
  
  {
    tab1[i][1]=tab2.getValueAt(i,2);
}

but it returns "illegal start of expression " error. So how can I transfer a table column values to a one column table, and what is wrong with my code?

但它返回“非法开始表达”错误。那么如何将表列值传输到一个列表,以及我的代码有什么问题?

1 个解决方案

#1


If you want to change the data in the table then you change the data in the TableModel, not in an Array. You do this be invoking the setValueAt() method of the JTable (or the TableModel):

如果要更改表中的数据,则更改TableModel中的数据,而不是数组中的数据。你这样做是调用JTable(或TableModel)的setValueAt()方法:

for (int i = 0; i < table2.getRowCount(); i++)
{
    Object value = table2.getValueAt(i, ?);
    table1.setValueAt(value, i, ?);
}

#1


If you want to change the data in the table then you change the data in the TableModel, not in an Array. You do this be invoking the setValueAt() method of the JTable (or the TableModel):

如果要更改表中的数据,则更改TableModel中的数据,而不是数组中的数据。你这样做是调用JTable(或TableModel)的setValueAt()方法:

for (int i = 0; i < table2.getRowCount(); i++)
{
    Object value = table2.getValueAt(i, ?);
    table1.setValueAt(value, i, ?);
}