Link to home
Start Free TrialLog in
Avatar of newbie_coder
newbie_coderFlag for Canada

asked on

EASY: Set Container Color, How?

How come this doesn't work?

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

It still displays an ugly gray colored background.

Help?
Avatar of vemul
vemul

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 :)
Avatar of newbie_coder

ASKER

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(
Avatar of CEHJ
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.
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);
 }

}

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.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>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?
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!
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.