Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to use TableCellRenderer for JButton.class

 
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use or add a JButton in a particular column but I am not extending the AbtractTableModel , so unable to use the JButton in JTable. In the code given , I am trying to make the 2nd column of the JTable as JButton.class and want to add JButton over there , but I am unable to proceed .

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things:

1. Don't have to put a JButton instance into the TableModel. TableModel contains data, JButton is a way to display something or a way to take input from the user, has nothing to do with the model.
2. You use table.getDefaultCellRenderer(JButton.class), but there is no default cell renderer for JButton.class, so it uses the String cell renderer in place.

For that thing to work you should create a class which implements TableCellRenderer, implement all the required methods and pass an instance of that class to setCellRenderer
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aurelian Tutuianu wrote:2. You use table.getDefaultCellRenderer(JButton.class), but there is no default cell renderer for JButton.class, so it uses the String cell renderer in place.


Technically it uses the Object cell renderer, but it does just use toString() of the values to display.
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everywhere I am getting the example with extending AbstractTableModel , but I am not using that here so unable to find out how to use getClass method .so can you please post the class and the functions need to be implemented here so the posted example can be worked out.
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to override the TableModel#getColumnClass()
 
Aurelian Tutuianu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Prime wrote:Technically it uses the Object cell renderer, but it does just use toString() of the values to display.

Right. I was thinking only at the fact that he can't have default CellRenderer for JButton class. A said String just because of toString call from inside, sorry for my lack of attention.
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of You, Now the code worked ..as below ..






 
Rancher
Posts: 3204
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to use or customize the Table Button Column
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your valuable suggestion
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic