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...
i can change the color of the frame but not the panel itself...
I don't think JPanel has a constructor with the specified Color
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Make sure you call setOpaque(true) on it
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();
}
}
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(
this.setSize(100,100);
this.show();
}
public static void main(String [] args){
new JPanelColor();
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.