Solved

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

Posted on 2011-02-19
7
638 Views
Last Modified: 2012-05-11
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...
0
Comment
Question by:wrynn
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34934693
I don't think JPanel has a constructor with the specified Color
0
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 34934710
If you first create JPanel and then setBackground like

JPanel p = new JPanel();
p.setBackground(Color.RED);

I think this should work; did you try it this way?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34934925
Make sure you call setOpaque(true) on it
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:wrynn
ID: 34934948
ya i've already tried both of those..
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34935082
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();
    }

}

0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 34935247
you will need to also set the background colour of any non transparent components you add to the panel as they are painted over the panel
or if you are not adding any to it make sure the layout manager you are using actually gives the panel the size that you expect.
0
 

Author Closing Comment

by:wrynn
ID: 36265772
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.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
String array comparison 4 45
dao vs facade design patterns 2 30
Java List 4 33
netbeans can not run self signed app 8 7
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question