aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to change the colour of an JTable Cell dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to change the colour of an JTable Cell dynamically" Watch "How to change the colour of an JTable Cell dynamically" New topic
Author

How to change the colour of an JTable Cell dynamically

hafz Khan
Greenhorn

Joined: Jul 04, 2010
Posts: 16
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

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Jul 04, 2010
Posts: 16
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

Joined: Jul 04, 2010
Posts: 16
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.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

For better help sooner, post a properly formatted SSCCE using the code tags.


luck, db
There are no new questions, but there may be new answers.
hafz Khan
Greenhorn

Joined: Jul 04, 2010
Posts: 16
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

Joined: Jul 04, 2010
Posts: 16
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.
satya anvesh
Greenhorn

Joined: Sep 23, 2008
Posts: 16
Hi hafz khan

can you post the solution you found for this problem?

Many thanks
Anvesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to change the colour of an JTable Cell dynamically