This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Show JButton with ImageICon in a JTable Cell  RSS feed

 
Jane Green
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, everybody:
I am new to the forum. I am trying to develop JButtons that with image icons in a JTable cells. And want to addActionListener to the buttons in the cell.
But so far, I could either be able to show imageicons on button but not be able to add actionListener to it, or can have actionListener functioning but shows the default gray buttons. Does anybody has experience on it, it would be great if u can post your code. (Also couldn't adjust teh button cell column width). Those CellRender and CellEditor are very hard for me to understand, does anybody has a good suggestion of reference book or web site?
Thanks a lot and have a good weekend.
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class ButtonRenderer extends JButton implements TableCellRenderer
{
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
{
setIcon(icon);
return this;
}
}

use tableColumn.setCellRenderer(new ButtonRenderer());
 
Jane Green
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. Just came back from vacation and got your reply.
It helps. I tried to put 'setIcon' in ButtonEditor, never tried to put it in ButtonRender, it's working now.
But there's other problems: 1. I can't set the width of the column that has the button inside. Column.setPreferredWidth(50) doesn't work.
2. When I pressed the button, it shows the gray background although i've set the background to white.
Any idea about those?
Thanks a lot.
[ September 17, 2003: Message edited by: rabbit grass ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"rabbit grass" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

Thanks! and welcome to the JavaRanch!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!