hi all . i have a little problem .
i have to change the image icon of the particular button in jable when i click on it. and have to rechang its icon and text when i click on it again .
so far m able to change its text when i click on it for the first time but not able to change its image .and also i dnt hvae any idea how to change the image and icon back to original when i click on it again .... commenting will e greatly appreciated his is the class where i supposed to do all that stuff
sohaib maroof
Greenhorn
Joined: Sep 28, 2011
Posts: 18
posted
0
i tried to change the text and icon through actionlisner , changing text is fine but the icon is not changing . here is my code
don't know the best way, but these steps seemed simple enough
using your code from the original source (Java2s)
in class JButtonTableExample:
add an int[] array equal to # of rows
add a mouseListener to the table
in mouseReleased, if column is the button column, intArray[row]++
pass a reference to the buttonRenderer (in the constructor) of the intArray[]
in class ButtonRenderer:
add a field int[], and pass it the reference from the constructor
create an ImageIcon[] of the 2 states you want
e.g. image1 and image2, or null and image1
in getTableCellRendererComponent(..) add this line:
setIcon(imageIconArray[intArray[row]%2]); //where 'row' is one of the parameters of the method
now, each time you click one of the table buttons, it's icon will alternate between
image1 and image2, or nothing and image1 (depending on how you set up the ImageIcon[])