EASY: Set Container Color, How?

newbie_coder
newbie_coder used Ask the Experts™
on
How come this doesn't work?

Container c = getContentPane();
c.setBackground(Color.white);

It still displays an ugly gray colored background.

Help?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
add a component to this.. something like JPanel and then set the background.. for ex:

Container c = getContentPane();
JPanel panel = new JPanel();
panel.setBackground(Color.white);

c.add(panel);
try this:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Frame1 extends JFrame {
  JPanel contentPane;

  public Frame1(){
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(null);
    this.setDefaultCloseOperation(3);
    this.setSize(new Dimension(400, 300));
    contentPane.setBackground(Color.white);
  }
}
Oh man, i am few minutes late! ha :)
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Author

Commented:
It works using a panel, but I have shapes and objects to add to the container before and it shoulds up, but after I add the panel you suggested, nothing shows up but a white window.

==================================

public class Frame1 extends JFrame
{
  JPanel c;

  public Frame1()
  {
    c = (JPanel) this.getContentPane();
    c.setLayout(null);
    this.setDefaultCloseOperation(3);
    this.setSize(new Dimension(400,600));
    c.setBackground(Color.white);
    c.add(new MyCircles());
  }
}

class MyCircles extends JPanel
{
  public void paintComponent(Graphics g)
  {
    // Contains shapes and objects, unrelated to this question
  }
}

==================================

So yeah, for the above, if I just have a regular container, it'll show all my objects in the MyCircles class, but with a gray background. Adding the panel you suggested, all the shapes disappears.

:o(
Top Expert 2016

Commented:
You have to decide whether you want your painting to occur on the frame or on the MyCircles panel. The latter would make you code more reusable.
Mick BarryJava Developer
Top Expert 2010

Commented:
I get a white background when I run the code you posted above. Check that it's not the MyCircles class that is painting the grey background.
try:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Frame1 extends JFrame
{
 JPanel c;
 MyCircles cir = new MyCircles();

 public Frame1()
 {
   c = (JPanel) this.getContentPane();
   c.setLayout(null);
   this.setDefaultCloseOperation(3);
   this.setSize(new Dimension(600,600));
   cir.setBounds(new Rectangle(10,10,500,500));
   c.setBackground(Color.white);
   c.add(new MyCircles());
   c.add(cir);
 }
}
----------------------------------------------------------
import java.awt.*;
import javax.swing.JPanel;

public class MyCircles extends JPanel {


  public void paintComponent(Graphics g)
 {
   g.drawOval(20,20,50,50);
 }

}

Author

Commented:
Okay, that works, but all my circles and shapes are filled with gray colors, which means it didn't really work, I could fill the objects with white, but that would be cheating and the code would be longer.

Ahhh, I thought this would be easy! I guess I'll increase the points.
Java Developer
Top Expert 2010
Commented:
public void paintComponent(Graphics g)
{
  g.setColor(Color.green);
  g.fillOval(20,20,50,50);
}
>>all my circles and shapes are filled with gray colors

??
i tried the codes and the circles is filled with white color.

so, just try objects's suggestion :)
question: are u trying to draw shapes on white color background?

do u mind post your MyCircles class coding?

Author

Commented:
Nevermind!

I added:

g.setColor(Color.white);
g.fillRect(0,0,400,600);

before all my other circles and objects, now it displays a white background, and I can keep my container. Thanks for your help anyways!
Mick BarryJava Developer
Top Expert 2010

Commented:
As I said in my previous comment:

> get a white background when I run the code you posted
> above. Check that it's not the MyCircles class that is
> painting the grey background.
alright then :)
newbie_coder:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
Mick BarryJava Developer
Top Expert 2010

Commented:

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial