wood burning stoves*
The moose likes Beginning Java and the fly likes changing background color of JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "changing background color of JFrame" Watch "changing background color of JFrame" New topic
Author

changing background color of JFrame

saurabh agr
Ranch Hand

Joined: Apr 14, 2010
Posts: 37
is it important to use getContentPane() method to change background of JFrame? like

class MyFrame extends JFrame
{
MyFrame()
{
getContentPane().setBackground(Color.red);
}
}

can we not change the background of JFrame without using ContentPane object?
pete reisinger
Ranch Hand

Joined: Dec 30, 2009
Posts: 46
It is important if you use older (Java) version, otherwise you don't (don't have to) use it.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

saurabh agr wrote:can we not change the background of JFrame without using ContentPane object?

What happened when you tried it?
saurabh agr
Ranch Hand

Joined: Apr 14, 2010
Posts: 37
if my code is like this, background is not changed....

class MyFrame extends JFrame
{
MyFrame()
{
setBackground(Color.red);
}
}

but if i use .....

class MyFrame extends JFrame
{
MyFrame()
{
getContentPane().setBackground(Color.red);
}
}

background is now changed..... i am using java 1.6..
is it important to use getContentPane() or is there any other way to change the background of JFrame?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
saurabh agr wrote:
background is now changed..... i am using java 1.6..
is it important to use getContentPane() or is there any other way to change the background of JFrame?


I think the safest bet is to work with the contentPane, but to be careful that components added to it have the appropriate opaque setting so as not to draw over the image.

You can, for example, paint in the contentPane:
saurabh agr
Ranch Hand

Joined: Apr 14, 2010
Posts: 37
pete reisinger wrote:It is important if you use older (Java) version, otherwise you don't (don't have to) use it.


sir ji a m using java 1.6 but background will not change until i use getContentPane()
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> but background will not change until i use getContentPane()

why don't you try to find out exactly what a contentPane is, and how it is used
Colin Wright
Greenhorn

Joined: Apr 21, 2010
Posts: 8
I can confirm having just tried it with JAVA 1.6 that you need to use getContentPane().setBackground(Color.red);

Normaly with this version of JAVA the compiler will imply in some cases that you mean to refer to the property of the ContentPane (a JPanel instance) rather than the JFrame itself, to provide a sort of shortcut to the programer.

However as you've just seen this isn't a good habbit to get into because it doesn't work with every method of a JPanel as you have illistrated and your code will not work if compiled on older versions of JAVA in anycase.

Just to add it's also not a good idea because it causes confusion over what object's method you are really calling as well.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: changing background color of JFrame