• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to change the colour of an JTable Cell dynamically

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Is it possible to change the colour of the JTable Cell Dynamically ?
i have set different colour for certain rows by overriding the prepareRenderer (TableCellRenderer renderer, int index_row, int index_col) in the JTable as below:



and now i have to set different colours for certain cells according to some condition what should i do for that ?
i tried by extending the DefaultTableCellRenderer class as below:



and setting it to JTable by



but i can see the color only when i am selecting the cells . As i am new i don't know whether i am doing anything wrong..

Your comments are really really appreciated.
Thanks a lot,
Hafz

 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Sun Swing JTable tutorial shows you how to do this with a custom TableCellRenderer. I've not seen this done by using prepareRenderer.

Please see: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
 
hafz Khan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI pete stein ,

Sorry i was not able to give different colors to certain rows by custom TableCellRenderer..could you please give an example to do it.
before i was able to colour certain rows using prepareRenderer.but the problem was that i was not able to colour certain cells after that by using custom TableCellRenderer...can you please help me out..

Thanks,
Hafz.
 
hafz Khan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi pete stein ,

sorry to say that i was not able to change the cell color in JTable using the custom TableCellRenderer. but i can see the changed color only when i am selecting the cells..what i have to do to see the colour without selecting the coloured cells..
the code which iam using is given below.

public class MyTableCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(column > 0){
if(row == 0 && column == 5){
comp.setBackground(new Color(204,255,204));
} else if(row == 4 && column == 5) {
comp.setBackground(Color.CYAN);
} else {
//comp.setBackground(Color.RED);
}
}
}

return comp;
}
}

could you please look the code above ...

Thanks,
Hafz.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For better help sooner, post a properly formatted SSCCE using the code tags.
 
hafz Khan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Darryl Burke,

I have posted the code above..i will explain my problem first.
I have to give different colours for certain rows in the table according to certain condition..
after that i have to colour certain cells as red for showing that it is locked..and also user have to lock the new cells by changing its color to red while clicking an button..

I have set different colour for certain rows by overriding the prepareRenderer (TableCellRenderer renderer, int index_row, int index_col) in the JTable as below:

1. table= new JTable(myDefaultTableModel) {
2. public Component prepareRenderer (TableCellRenderer renderer, int index_row, int index_col){
3. Component comp = super.prepareRenderer(renderer, index_row, index_col);
4.
5. if( index_row < SOME_CONDITION ){
6. comp.setBackground(new Color(204,255,204));
7. } else {
8. comp.setBackground(Color.CYAN);
9. }
10.
11. if(isCellSelected(index_row, index_col)){
12. comp.setBackground(new Color(0, 0, 112));
13.
14. }
15. return comp;
16. }
17. };

now i have to set different colours for certain cells to show it is locked(red colour)...and also while clicking an button user may be able to change the colour of the selected cell to red(locked)..

can you please tell how i should do it..
the same code is written above in this post..

Thank You,
Hafz.
 
hafz Khan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks for your valuable help and support.
I was not able to solve this by using custom TableCellRenderer . but anyway have solved the issue in another way.

Thanks to all,
Hafz.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi hafz khan

can you post the solution you found for this problem?

Many thanks
Anvesh
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic