Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Obtaining row id in JTable

 
Simon Knight
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - I have a JTable with a column of buttons on the right. When you push a button on a row, the button needs to extract data from the row and send it somewhere else. My problem is, I can't work out how to identify the row identity in which the button was pushed. Can anyone help?
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Table rows don't really have ID's. They have indexes. So row 5 of a table is index 4 because the array starts at 0.

Assuming you have a custom cell editor and you've implemented the following method:

public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)

You've got the index of the row already as well as the column. At that point you might get the value from from your data array (default in JTable is a Vector but I always use something better, like a List) at the same index.

Each row in the JTable should have the same index as your model's data.
 
Simon Knight
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Er - I haven't got a custom cell editor... Is one necessary, or is there another way?

What I was trying to achieve was a method called on the table in which the buttons reside which would provide me with the row number in which the button was clicked, and I would then use this info to call getValueAt(int rowIndex, int columnIndex) in my abstractTableModel implementation. Is this possible?
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Simon Knight:
Er - I haven't got a custom cell editor... Is one necessary, or is there another way?

What I was trying to achieve was a method called on the table in which the buttons reside which would provide me with the row number in which the button was clicked, and I would then use this info to call getValueAt(int rowIndex, int columnIndex) in my abstractTableModel implementation. Is this possible?


How are you putting a JButton in a cell?
 
Simon Knight
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using tableModel, in conjunction with my own renderer class implementing TableCellRenderer. This doesn't work too well admittedly as the buttons don't yet show any visual evidence of having been pressed - this is just a cosmetic issue though and as such I hoped to be in a position to address it later once I'd gotten the core functionality sorted.

The relevant code is:



and



and



TimelinejTable.getSelectedRow() returns a value of -1 due to there being no actual row selected because instead of clicking on a cell, the user clicks on a button instead, which is where I believe the problem lays. Any advice?
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem is that you are using a renderer to display the button. You should use an editor instead. That way, when you click the button, the table actually is notified that it is being edited and you will have the selected row as you desire.
 
Simon Knight
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fair enough, and thanks! - I note however that the getTableCellEditorComponent() method takes the row and column values as arguments though, with the row value in particular being the root of all my issues at present. Have you any advice on how to implement the editor class so that I can provide it with the necessary arguments?
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Simon Knight:
Fair enough, and thanks! - I note however that the getTableCellEditorComponent() method takes the row and column values as arguments though, with the row value in particular being the root of all my issues at present. Have you any advice on how to implement the editor class so that I can provide it with the necessary arguments?


You are merely implementing that method and you won't feed it anything. The table will do that for you. Just like it does for the renderer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic