Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

changing background color of JFrame

 
saurabh agr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is important if you use older (Java) version, otherwise you don't (don't have to) use it.
 
Paul Clapham
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic