This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes setting up cell in JTable as a JButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "setting up cell in JTable as a JButton" Watch "setting up cell in JTable as a JButton" New topic
Author

setting up cell in JTable as a JButton

Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Hi Guys,

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.

Thanks,

Alan

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
A renderer is not a real component. It is just a painted image of a component. It does not receive events, so I doesn't respond to any mouse events.

You need to create an editor.

Table Button Column is a renderer/editor in one class.
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Hi,

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.

Alan
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
I gave you working code. I don't know why you are trying to split it up into two classes. Good luck.
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
OK. I consolidated everything into one class as suggested and as per example: http://tips4java.wordpress.com/2009/07/12/table-button-column/. However, there is still a problem. I can only click on one button at runtime! Any other button I try to click on seems disabled??? Please advise. See code below:

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
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.
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
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,

Alan
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setting up cell in JTable as a JButton