Solved

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

Posted on 2011-02-19
7
639 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 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaFX TableView not displaying correctly 3 164
eclipse console opening separately 2 50
ejb mdb examples 1 39
Notify sent to other threads in Java 9 54
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

752 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