Difference between object JDialog.getContentPane (). setBackground () and object JDialog.setBackground ()

1

The question is simple, I want to know why with the getContentPane () the color change of the background occurs and without the no.

    NotaJDialog njd = new NotaJDialog (this,true);
    njd.setResizable(false);
    njd.getContentPane().setBackground(Color.red);
    njd.setBackground(Color.yellow);
    njd.setVisible(true);
    
asked by Ronald Sanchez 16.04.2017 в 16:23
source

2 answers

0

Good, when you put njd.getContentPane() you are calling the container of NotaJDialog , that is, if you have entered the NotaJDialog within a JPanel when you put the first instruction of your question you are changing the background color in the father of NotaJDialog .

On the other hand, in the other instruction you are changing the color to NotaJDialog .

I'll leave the official documentation .

We can only change-by calling the container

.getContentPane() 

link enter the description of the link here

    
answered by 16.04.2017 в 16:43
0

Well I answer myself since I think the answer is this.

First to know that a JFrame is formed or composed of several layers. As you put it in this image

Where the components are really located is in this container called ContentPane and as it clearly shows in the image it is the one that is really worked on. Therefore, in order to add or make modifications, this container is made. So how do I access my container and be able to change, for example, the background color? Well, using the getContentPane method. From the newly I can change the background of the JFrame or JDialog that comes to be almost the same. Example:

  NombreJFrame objetoJFrame = new NombreJFrame (this,true);
  objetoJFrame.setBackground(Color.red); // De esta forma no estamos cambiando el color de fondo ya que no estamos trabajando sobre el contenedor donde se ubica los elementos
  objetoJFrame.getContentPane().setBackground(Color.blue); // Como estamos obteniendo el contenedor del JFrame y es realmente donde se trabaja con los elementos, si que estariamos cambiando el color de fondo.

Here I leave another image to be able to clarify it more.

    
answered by 16.04.2017 в 19:37