• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

changing background color of JFrame

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is important if you use older (Java) version, otherwise you don't (don't have to) use it.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic