Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use TableCellRenderer for JButton.class

 
buntha Choudhary
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 .

 
Aurelian Tutuianu
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
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java 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.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 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 ..






 
Rob Camick
Ranch Hand
Posts: 2703
10
  • 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
 
Poop goes in a willow feeder. Wipe with this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!