What is the relation between ContentPane and JPanel?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-03-12T13:05:45Z Indexed on 2010/03/12 13:07 UTC
Read the original article Hit count: 300

Filed under:
|
|
|

I found one example in which buttons are added to panels (instances of JPanel) then panels are added to the the containers (instances generated by getContentPane) and then containers are, by the construction, included into the JFrame (the windows).

I tried two things:

  1. I got rid of the containers. In more details, I added buttons to a panel (instance of JPanel) and then I added the panel to the windows (instance of JFrame). It worked fine.

  2. I got rid of the panels. In more details, I added buttons directly to the container and then I added the container to the window (instance of JFrame).

So, I do not understand two things.

  1. Why do we have two competing mechanism to do the same things.

  2. What is the reason to use containers in combination with the panels (JPanel)? (For example, what for we include buttons in JPanels and then we include JPanels in the Containers). Can we include JPanel in JPanel? Can we include a container in container?

© Stack Overflow or respective owner

Related posts about java

Related posts about gui