how to get handle of row when JButton in JTable is pressed

I am finally starting to understand the whole renderer, editor aspect of a JTable. I've added buttons to the last column of my JTable, and did all the necessary work in the editor and renderer so that the text in the last column actually renders into a button, and behaves like a button.

My ultimate goal is to have the button, when pressed, delete that row. But I am stuck on something, how do I grab a handle of the row of data that the button correlates to. Right now i have a handle to the actionEvent but all it seems to know is that it was a button and the physical location. I mean I can do some arithmetic and figure out which row, maybe, that button is on, using row heights and all that stuff, but that seems really hacky. Is there a way to get info about the row that the clicked-button relates to?

[823 byte] By [deadseasquirrelsa] at [2007-10-3 10:51:49]
# 1

I think I figured out some of it. Currently my Renderer, Editor, and ActionListener are all the same class. It's a one stop shop. Because of this, I can set a handle to the table, and also a handle to the selected row from the constructor which takes these parameters: (TableCellRenderer renderer, TableCellEditor editor, JTable table), and also from this Editor method:

public Component getTableCellEditorComponent(JTable table, Object value,

boolean isSelected, int row, int column){

does that sound like the correct approach. Right now I don't have fields within this one-stop-shop class, but perhaps I should have fields that get a handle to these components?

deadseasquirrelsa at 2007-7-15 6:17:13 > top of Java-index,Desktop,Core GUI APIs...
# 2

> I think I figured out some of it. Currently my Renderer, Editor, and ActionListener are all the same class.

Two weeks ago you where given a link to code that does exactly this:

http://forum.java.sun.com/thread.jspa?threadID=785192

> how do I grab a handle of the row of data that the button correlates to

> Right now i have a handle to the actionEvent but all it seems to know is that it was a button

See above.

Another way is to set the actionCommand of the button in the getCellEditorComponent(..) method to be the value of the row you are currently editing.

camickra at 2007-7-15 6:17:13 > top of Java-index,Desktop,Core GUI APIs...
# 3

I have been looking at that post, but it doesn't show or discuss in detail how to remove a selected row from a JTable. You explain how to make the button disappear, but from everything I've read people are saying to use hte removeRow() method on the TableModel. But the API doesn't show a removeRow() method on the tableModel. I thought it might be the removeRowSelectionInterval(), but htat didn't work for me either. So it seems like i'm barking up the wrong tree.

deadseasquirrelsa at 2007-7-15 6:17:13 > top of Java-index,Desktop,Core GUI APIs...
# 4

> I have been looking at that post, but it doesn't show or discuss in detail how to remove a selected row from a JTable.

Your question in this posting was:

how to get handle of row when JButton in JTable is pressed

The above posting deals with that question.

> but from everything I've read people are saying to use hte removeRow() method on the TableModel

Its a method of the DefaultTableModel.

camickra at 2007-7-15 6:17:13 > top of Java-index,Desktop,Core GUI APIs...