[英]How can I get selected row values from a jTable and add that values to another jTable?
I tried to create a jTable
by adding a column of type boolean
, to tick the wanted rows. 我试图通过添加类型为
boolean
的列来创建jTable
,以选中所需的行。 And get them into another similar jTable
. 并将它们放入另一个类似的
jTable
。
I used jTable1.getModel().setValueAt(int, int, int);
我使用了
jTable1.getModel().setValueAt(int, int, int);
but can't put selected String
values to the value
parameter. 但不能将选定的
String
值放入value
参数。
Can someone help me please? 有人能帮助我吗?
I used jTable1.getModel().setValueAt(int, int, int);
我使用了jTable1.getModel()。setValueAt(int,int,int); but can't put selected String values to the value parameter.
但不能将选定的String值放入value参数。
Sure you can, the signature is setValueAt(Object, int, int)
. 当然可以,签名是
setValueAt(Object, int, int)
。 The first parameter is Object
, not "int" so you can put any Object into a TableModel. 第一个参数是
Object
,而不是“ int”,因此您可以将任何Object放入TableModel中。
If you are talking about adding new rows of data to the second table then you need to use the addRow(...)
method of the DefaultTableModel
of your table. 如果您正在谈论向第二个表添加数据新行,则需要使用表的
DefaultTableModel
的addRow(...)
方法。 That is, the DefaultTableModel will initially contain no data so you can't just use the setValueAt(...) method. 也就是说,DefaultTableModel最初将不包含任何数据,因此您不能只使用setValueAt(...)方法。 Instead you need to add a new row of data for every row that is selected in the first table.
相反,您需要为第一个表中选择的每一行添加一个新的数据行。
If you need more help than post your SSCCE that demonstrates the problem. 如果您需要更多的帮助,而不是发布表明问题的SSCCE 。
JTable is very flexible and could add and delete any row you desire. JTable非常灵活,可以添加和删除所需的任何行。
The number of columns that constitute a row may vary from table to another so JTable has a model object through which you can manipulate the rows of the table. 构成一行的列数可能因表而异,因此JTable具有一个模型对象,您可以通过该对象来操纵表的行。
The JTable
will have a DefaultTableModel
. JTable
将具有DefaultTableModel
。 You can add rows to the model with your data. 您可以将行与数据一起添加到模型中。
If you get the column values of a selected row of a JTable for example: 例如,如果获得JTable的选定行的列值:
String obj1
for row1, String obj2
for row2, String obj3
for row3, ...ect String obj1
,第2行的String obj2
,第3行的String obj3
obj3,... ect
OR IF YOU NEED BOOLEAN : 或如果您需要布尔:
Boolean obj1
for row1, Boolean obj2
for row2, Boolean obj3
for row3, ...ect Boolean obj1
,第2行的Boolean obj2
,第3行的Boolean obj3
obj3,... ect
Then you can make the following: 然后,您可以进行以下操作:
Object[] row = { obj1 , obj2 , obj3 };
You get the object model of the destination table as follows: 您将获得目标表的对象模型,如下所示:
DefaultTableModel model = ( DefaultTableModel ) jTable1.getModel();
Then add the manually created row to it: 然后向其中添加手动创建的行:
model.addRow( row );
And that's it! 就是这样!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.