This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization 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
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

JTable Color Change

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. How do I put different colours in different rows like first row red color, second row blue and so on....and how to do the same thing but with individual cells of a JTable.
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi prakash,
to play with colours u need to add a renderer to ur table. the code goes like this.
JTable myTable = new JTable(.....);
first of all u need to write a renderer class example is
class gridTableCellRenderer extends JLabel implements TableCellRenderer{
/// inside getTableCellRendererComponent function write all
/// the color things u want
public Component getTableCellRendererComponent( JTable table,
Object value, boolean isSelected,
boolean hasFocus, int row, int column )
{
if(row == 2)
setBackground(Color.green);
else
{
if(row == 4 && column == 1)
setBackground(Color.red);
}
}
}
////add renderer for every type of colunms
//// that is if u have one column of type JLable, & other of type
/// Integer then u'll write
myTable.setDefaultRenderer(JLabel.class,new gridTableCellRenderer());

hope this helps
deekasha
 
Prakash Radhakrishan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi deekasha gunwant,
Thank You for the response. It works fine.
prakash,
 
The moustache of a titan! The ad of a flea:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!