0

When you create JFrame instance, you have setBackground method available from this instance. However, regardless to what color you try to put there, you`ll receive gray background color.

This happens (as i understood), because default JPanel instance is automatically created inside JFrame and lays over it . So, to get color set, you need to call

JFrame.getContentPane().setBackground(Color.RED);

that actually calls setBackground of default JPanel , that exists inside JFrame. I also tried to do next :

JFrame jf = new JFrame();

//I expect this will set size of JFrame and JPanel 
jf.setSize(300, 500);

//I expect this  to color JFrame background  yellow 
jf.setBackground(Color.yellow);

//I expect this to shrink default JPanel to 100 pixels high, 
//so 400 pixels of JFrame should became visible
jf.getContentPane().setSize(300, 100);

//This will make JPanel red
jf.getContentPane().setBackground(Color.RED);

After this set of code, I am having solid red square of JFrame size , i.e. 300 x 500. Questions :

  1. Why jf.getContentPane().setSize(300, 100); does not resize default JPanel , revealing JFrame background?

  2. Why JFrame has setBackground method if anyway you cannot see it and it is covered by default JPanel all the time?

Braj
  • 46,415
  • 5
  • 60
  • 76
Danylo Gurianov
  • 545
  • 2
  • 7
  • 21
  • Note that even if the background was set to yellow, it would be set to red just 1 statement later. – BitNinja Jun 13 '14 at 20:39
  • I consider those two as different methods of separate objects: jf.setBackground(Color.yellow); and jf.getContentPane().setBackground(Color.RED); Am i wrong ? – Danylo Gurianov Jun 13 '14 at 20:41
  • Read the section from the Swing tutorial on [Using Top Level Containers]() for information about the structure of a frame. You should not be tying to set the size of any component. That is the job of the layout manager, so even if you set the size of the content pane the layout manager will override the size. – camickr Jun 13 '14 at 23:23

1 Answers1

2

As per the class hierarchies of JFrame as shown below:

java.lang.Object
    java.awt.Component
        java.awt.Container
            java.awt.Window
                java.awt.Frame
                    javax.swing.JFrame

The method Frame#setBackground() is inherited from Frame and JFrame doesn't do override it.

What JFrame states:

The JFrame class is slightly incompatible with Frame. Like all other JFC/Swing top-level containers, a JFrame contains a JRootPane as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame. This is different from the AWT Frame case.


You can override default setBackground() of JFrame as shown below:

@Override
public void setBackground(Color color){
    super.setBackground(color);
    getContentPane().setBackground(color);
}
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Just found out , that if i turn off visibility of root content pane, i will see yellow background. Resizing visible root content pane does not reveal part of background though. Am i expecting something proposterous? – Danylo Gurianov Jun 13 '14 at 21:11
  • yes its covered by content pane as documented. For component `setSize()` method works based in Layout manager. – Braj Jun 13 '14 at 21:11