This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get index value of a row in JTable?  RSS feed

 
Shubham Rungta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shubham Rungta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shubham Rungta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks I got the solution.

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

Thanks Again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!