This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Obtaining row id in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Obtaining row id in JTable" Watch "Obtaining row id in JTable" New topic
Author

Obtaining row id in JTable

Simon Knight
Greenhorn

Joined: Mar 04, 2004
Posts: 25
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

Joined: Jul 11, 2001
Posts: 15286
    
    6

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.


GenRocket - A Test Data Generation Platform
Simon Knight
Greenhorn

Joined: Mar 04, 2004
Posts: 25
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

Joined: Jul 11, 2001
Posts: 15286
    
    6

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

Joined: Mar 04, 2004
Posts: 25
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

Joined: Jul 11, 2001
Posts: 15286
    
    6

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

Joined: Mar 04, 2004
Posts: 25
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

Joined: Jul 11, 2001
Posts: 15286
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Obtaining row id in JTable
 
Similar Threads
JTable(!!!)
JTable ........
JTable
jtable
JTable