Click to See Complete Forum and Search --> : Color Changing, JTable


sat
August 13th, 1999, 07:28 AM
Hello

I Created a Frame. On TOp of the Frame I had a Button "Change". Under That I had a Table with 10 row's with White Back Ground ( default renderer).

CASE1>>IF I Press Change button,

1)First 5 rows Should display with RED Color(i.e red BackGround)
2) Next 3 rows should display with Blue Color(i.e Blue BackGround)
3) Next 2 rows should display with Green Color(i.e Green BackGround)


CASE2>>If Again I press Change Button,

Again I should Get Original State. I.e With White BackGround.
(Default renderer)

How TO Switch case1 to case 2 or case2 to case1..Like above.......

Please give an example........

REGARDS
CHANDRA

unicman
August 13th, 1999, 09:00 AM
U can create a custom renderer. This renderer will maintain state 'DEFAULT', 'COLORED'.

Now the renderer be probably 'JLabel' only. The only thing is u will check the state and then decide whether to color the background. If no, just do 'setOpaque(false)'. If yes, check the row index and give the background color accordingly.

Now to change the state of renderer...
1. U will have a function to the renderer class say 'toggleState' This will toggle the state.
2. In the handler of change button, u will call this method. (For this u will need to store the object of the renderer class in the class having handler for the button, or u can retrieve the renderer and typecast it)

Does this answer ur question?

- UnicMan
http://members.tripod.com/unicman