Forums Register Login

How to get index value of a row in JTable?

+Pie Number of slices to send: Send
Hi,

Can anyone know how to get index value of a row in jtable.
I am trying to build a code where I want to add a row in between.
For this I need to know the index value of already inserted row.

Is it possible?


Thanks & Regards,
Shubham Rungta
1
+Pie Number of slices to send: Send
Welcome to the Ranch.

You don't add a row. You add the row data, typically using DefaultTableModel#addRow(Object[] rowData)
From the API docs for the above method

Adds a row to the end of the model.



If you want to insert add row data, at a specific index, you can use
DefaultTableModel#insertRow(int row,Object[] rowData)

Coming back to your question

For this I need to know the index value of already inserted row.


So how are you building your rows? If you start off with some default data, then the last row would be numberOfRows-1
+Pie Number of slices to send: Send
Hi,

Thanks for reply

Suppose I am having 5 rows, now at index 3, I want to insert a new row ( each time this index value may change). How to get this index value.

Through insertRow() I can insert row at random position. But I am bother about this random position.

What I want is, when I select a row, I should get its index value. So by clicking on insert row button(which I'll be having on my form), I can add row at random index.


Thanks

Shubham Rungta
+Pie Number of slices to send: Send
 

Through insertRow() I can insert row at random position. But I am bother about this random position.


It is not random. The first paramter specifies the row index

What I want is, when I select a row, I should get its index value. So by clicking on insert row button(which I'll be having on my form), I can add row at random index.


Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#selection
+Pie Number of slices to send: Send
Hi,

Thanks I got the solution.

Through " int insertRow=table_FM.getSelectedRow(); " I am able to get the index value of row.

Thanks Again
Do not threaten THIS beaver! Not even with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 5013 times.
Similar Threads
JTable ........
jtable
refreshing a JTable
JTable
Control JTable DnD cells?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:00:20.