Help--- JButton background color - Programmers Heaven

Howdy, Stranger!

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


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..

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


  • silveredge52silveredge52 Posts: 198Member
    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.