• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get index value of a row in JTable?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic