Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTableCellRenderer for one row  RSS feed

 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
The problem is: I need when row is selected then in first cell of this row must appear an icon. When the other row is selected in previous row icon must desapperar but in new selected row it must appear and so on.
How I can do this.

thanks in advance.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want the selected row to display an icon, but unselected rows not to? Easy. Extend javax.swing.table.DefaultTableCellRenderer. In the getTableCellRendeererComponent method, check the isSelected parameter. If it is true, add an icon (DefaultTableCellRenderer extends JLabel so just invoke setIcon()). If it is false, do whatever you want for the unselected rows.
When you create your JTable, use aTable.getColumnModel().getColumn(columnIndex).setCellRenderer() to associate a table column with your new renderer.
You should also either extend javax.swing.DefaultCellEditor to do the same thing DefaultTableCellRenderer does or make the column uneditable through your data model to make the table behave properly when someone clicks on the icon cell.
The Java Tutorial has other instructive examples in How to use Tables.
 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks A LOT, it works.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!