Solved

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

Posted on 2011-02-19
7
634 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now