简体   繁体   中英

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. And get them into another similar jTable .

I used jTable1.getModel().setValueAt(int, int, int); but can't put selected String values to the value parameter.

Can someone help me please?

I used jTable1.getModel().setValueAt(int, int, int); but can't put selected String values to the value parameter.

Sure you can, the signature is setValueAt(Object, int, int) . The first parameter is Object , not "int" so you can put any Object into a 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. That is, the DefaultTableModel will initially contain no data so you can't just use the setValueAt(...) method. 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.

JTable is very flexible and could add and delete any row you desire.

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.

The JTable will have a 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:

String obj1 for row1, String obj2 for row2, String obj3 for row3, ...ect

OR IF YOU NEED BOOLEAN :

Boolean obj1 for row1, Boolean obj2 for row2, Boolean obj3 for row3, ...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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM