wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes changing jtable button image and icon Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "changing jtable button image and icon" Watch "changing jtable button image and icon" New topic
Author

changing jtable button image and icon

sohaib maroof
Greenhorn

Joined: Sep 28, 2011
Posts: 18
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
i tried to change the text and icon through actionlisner , changing text is fine but the icon is not changing . here is my code
sohaib maroof
Greenhorn

Joined: Sep 28, 2011
Posts: 18
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

PatienceIsAVirtue.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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[])
sohaib maroof
Greenhorn

Joined: Sep 28, 2011
Posts: 18
 
 
subject: changing jtable button image and icon
 
Similar Threads
problem clicking jbuttons in jtable
JButton in JTable
Jtable filled with JButtons each having their own ImageIcon from a SQL server
JTable question - URGENT
Jbutton inside a jtable wont fire action