Help--- JButton background color

I have a JButton that when it is pressed, the background color of it changes. My problem now is how can I change it to its original color when it is pressed again? Here is my code so far..

JButton f= new JButton("G");
f.setBounds(62, 84, 45, 30);
f.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent arg0) {


  • Hey,
    Use couple of variables to keep track of prior background color, ie.
    [code] public void actionPerformed(ActionEvent arg0) {
    if (firstTime) {
    priorColor = Color.CYAN;
    firstTime = false;
    holdPriorColor = f.getBackground();
    priorColor = holdPriorColor;
    regards, se52
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion