How to set up a JRadioButton render?

I created a subclass of JRadioButton in which I override the paintComponent method as follows:

@Override protected void paintComponent(Graphics g) { g.drawImage( isSelected() ? getCheckedImg() : getBasicImg() , 0, 0, this); } 

but it seems that after the button is drawn, the image will be used forever. The isSelected test does not seem to have any effect. Graphics cached or something Java? How can I customize my JRadioButton with a selected and unselected image? Should I write a user interface?

+1
source share
3 answers

Read the API. There are methods such as:

 setIcon() setSelectedIcon() 

among others that you can use instead of doing custom coloring.

+4
source

To maintain functionality, it is not difficult to extend BasicRadioButtonUI and override the paint() delegate method. You can set a new user interface using setUI() .

+2
source

Even in Java swing, I usually override paint rather than paintComponent to customize the look. I believe that by default paint will call paintComponent , but only if the component needs to be repainted.

-3
source

All Articles