This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I'm working with a JTable and have created a TableCellRenderer that displays JButtons. See below. The buttons render perfectly, however, for whatever reason the actionPerformed method doesn't get triggered when I click on a button at run time. Could someone please advise what needs to change to make the code in the actionPerformed(..) method execute.
I've been playing with this for hours trying to get it to work. I created a seperate class named ButtonEditor. I invoke everything like so:
here's the ButtonEditor:
I have the ButtonRenderer being used on two seperate columns: 1 and 10. In column 1 I display a URL icon and in column 10 I have it display a person icon. Thus I've changed the ButtonRenderer a little:
I'm just confused all over... I'm assuming the method getTableCellEditorComponent(...) in ButtonEditor is going to give me an instance of ButtonRenderer through the value parameter (yes? no?), but at runtime this method isn't even being called. Also, in ButtonEditor I'm setting the addActionListener() method in the constructor of the class on the ButtonRenderer that is being passed. I don't know if that's the proper way to do this or not. Somebody please guide me.
Joined: Jun 13, 2009
I gave you working code. I don't know why you are trying to split it up into two classes. Good luck.
There is no need to customize the class I gave you.
The Action is passed to the class. You changed the constructor to ignore the Action.
The ImageIcon you want to display is added to the model.
The blog gives an example (for deleting a row from the model), of how to use the class the way it was designed to be used. The idea is to customize the Action that is invoked. If you want to customize the class then you are on your own.
Joined: Sep 24, 2003
If you would look once again at the code I've provided, you'll see I've not changed it much. I've reinstated your original constructor and have edited the actionPerformed(...) method slightly since I'm not interested in passing the row value to the Action openWebsite. I am, however, interested in passing the string value of the url variable in ButtonColumn. You can see in the Action openWebsite I call the e.getActionCommand() method to retrieve this value. As far as the images are concerned, they render just fine, as apposed to your setting the labels for the buttons. All this still has no bearing on my problem, however. At runtime I am still only able to click on one button. All the others seem disabled. I suspect there is a problem elsewhere in my code other than the ButtonColumn class. In my table model I have the isCellEditable(...) returning true, which should make every cell editable. I don't know where else I could have messed up??? Please advise,
Joined: Jun 13, 2009
For the last time, there is no need to make any changes to the class I provided. If you want to do this then don't ask me about the class.
All you need to change is the Action.
I'm not interested in passing the row value to the Action openWebsite.
Yes, you need the row. This is the way the class was designed to work. Once you have the row, you can query the TableModel to get the value stored in the model. Once you have the value from the model you can use it however you want. In your case the model should store the URL so you just get the value and invoke the Desktop class.