0

I want to extend the JButton class and manually paint the button's icon, though I need to get the correct icon according to the state the button is in, how do i do that?

Method getIcon() returns the default icon only, regardless of what state the button is in...

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
  • This could be harder than just painting an area and checking mouseListener if clicked on that area(between xmin xmax ymin ymax). Did you try to get screen pixel reading function? rgbArray = image.getRGB(startX, startY, w, h, rgbArray,offset, scansize) ; – huseyin tugrul buyukisik May 12 '13 at 15:25
  • no it is actually not harder, espicially when they already handle the buttons state positions layout etc. – Ofek Ron May 12 '13 at 15:27

2 Answers2

1

Instead, implement the Icon interface. ColorIcon, illustrated here, is a simple example used by a JButton subclass. Try instantiating ColorIcon for each of several colors . Use the Icon instances as the the button's pressed or rollover icon to see the effect. See also this related example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

getIcon() will return the default icon, but getDisabledIcon() will return the disabled icon.

Also getDisabledSelectedIcon() returns the icon used by the button when it's disabled and selected. If no disabled selection icon has been set, this will forward the call to the LookAndFeel to construct an appropriate disabled Icon from the selection icon if it has been set and to getDisabledIcon() otherwise. Some look and feels might not render the disabled selected Icon, in which case they will ignore this.

Then there is getPressedIcon(), getRollOverIcon() and getRolloverSelecedIcon().

Check the AbstractButton manual page.

As you can see there are many options available to you. On the other hand, you probably are the one who has set the icons, so if you may keep them in an Icon array, you could get them straight from there.

Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47