Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help--- JButton background color

molliemollie Posts: 6Member
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..

[code]
JButton f= new JButton("G");
f.setBounds(62, 84, 45, 30);
f.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {
f.setBackground(Color.CYAN);
}});
[/code]

Comments

  • silveredge52silveredge52 Posts: 198Member
    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();
    f.setBackground(priorColor);
    priorColor = holdPriorColor;
    }[/code]
    regards, se52
Sign In or Register to comment.