Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-19
7
Medium Priority
?
641 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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.
Suggested Courses

722 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