Q:

Java Swing JButton

I want to create button with custom look and feel. I have got different images to be set as background of button for normal, mouse over, mouse click, button disabled etc. I have created my own class, extending javax.swing.JButton and overrides paintComponent method. How can i change my button background for all the above given states.

A:

In addition to Steve De Caux's answer, you can:

Add a MouseListener which changes an enum variable, let's call it state on your extended JButton
In your overridden paintComponent take into consideration the current state and paint different backgrounds. Like
if (!getModel().isEnabled()) {
} else if (state == ButtonState.MOUSE_OVER) {
} else if (state == ButtonState.MOUSE_CLICKED) {   
}

A:

JButton has a series of simple set methods for rollover, pressed, selected, disabled and disabled selected states - for example
button.setPressedIcon(new ImageIcon("images/button-down.png")

the other methods are :
button.setRolloverIcon()
button.setSelectedIcon()
button.setRolloverSelectedIcon()
button.setDisabledIcon()
button.setDisabledSelectedIcon()

...have fun !
By the way, O'Reilly has a fun book called Swing Hacks with lots of little goodies for playing with swing : Swing Hacks

