This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

changing background color of JFrame  RSS feed

 
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?
 
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.
 
Sheriff
Posts: 23692
50
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?
 
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()
 
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
 
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
Boost this thread!