I am trying to override the default background colour of the JButton
while it is pressed; the background colour must stay as long as the button is still pressed.
I tried this code:
button.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent evt) {
button.setBackground(Color.BLUE); // applied after release!!
System.out.println("pressed"); // applied on pressed - OK.
}
});}
mousePressed
here doesn't achieve my requirement!!
This line is never invoked: button.setBackground(Color.BLUE);
. But this line is invoked: System.out.println("pressed");
.
However this line button.setBackground(Color.BLUE);
is invoked after releasing the button - Not while pressed!!!
How to achieve my requirement?