• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

setting up cell in JTable as a JButton

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic