Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-19
7
Medium Priority
?
643 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

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!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

972 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