简体   繁体   English

如何从jTable中获取选定的行值并将其添加到另一个jTable中?

[英]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. 如果您正在谈论向第二个表添加数据新行,则需要使用表的DefaultTableModeladdRow(...)方法。 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.

相关问题 如何从 JTable 复制行单元格值并转移到另一个 JTable - How to copy row cell values from a JTable and transfer to another JTable 获取所选JTable行中所有列的值 - Get values of all columns in selected JTable row 如何从jtable内的组合框中获取值和选定的值? - How to get values and selected values from combobox inside jtable? JTable 错误:我试图在文本字段中显示来自 JTable 的选定行值,但我不断收到此错误: - JTable error: I'm trying to display the selected row values from a JTable in the textfield but I keep getting this error: 如何在另一个窗口中使用JTable中的选定值填充Textfields? - How to populate Textfields with selected values from JTable on another window? 如果我单击jtable中的一行,如何更改微调器和单选按钮的值(必须取决于jtable的值) - How to change the values of spinner and radio button if I click on a row from jtable (must depend on the values form jtable) 如何在 java swing 中向 jtable 添加行值? - how to add row values to jtable in java swing? 如果选中了 jtable 中相应复选框的复选框,如何获取另一个单元格的值? - How do I get values of another cell if a checkbox is selected of the corresponding checkbox in jtable? 显示动态变化的JTable中选定行的值 - Display values of a selected row from a dynamically varying JTable 如何从jtable中获取字符串值? - How do I get string values from my jtable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM