Link to home
Start Free TrialLog in
Avatar of wrynn
wrynn

asked on

trying to change the background color of a JPanel but it has no effect

any ideas why i can set the back ground color for a frame but not for an individual panel? setting the panel color inside its constructor has no effect...

i can change the color of the frame but not the panel itself...
Avatar of for_yan
for_yan
Flag of United States of America image

I don't think JPanel has a constructor with the specified Color
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
Make sure you call setOpaque(true) on it
Avatar of wrynn
wrynn

ASKER

ya i've already tried both of those..
This simple code (see below) runs for me and shows whatever
color I set to JPanel.
Please, run this one - I'm sure it will run for you.
If it runs then post your code which does not show color to you.

import javax.swing.*;
import java.awt.*;

public class JPanelColor extends JFrame {
    JPanel p;

    public JPanelColor(){
        p = new JPanel();
        p.setBackground(Color.BLUE);
        this.getContentPane().add(p);
        this.setSize(100,100);
        this.show();
    }

    public static void main(String [] args){
        new JPanelColor();
    }

}

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wrynn

ASKER

I figured it out.  The problem was that I was using the repaint() method inside of my panel to draw on the panel using a double buffer graphics object and that was overriding my call to setBackground with its own background.