Why should I use java containers?

229 views Asked by At
  Container c = this.getContentPane();
  JLabel lbl = new JLabel("Label");
  c.add(lbl);

what is the difference between using the above method & this one ? knowing that both give same results

  JLabel lbl = new JLabel("Label");
  add(lbl);

& if I need to put many items in the frame I can just make many Panels and add them all to one mainpanel, I don't see any need for containers, sorry for such a basic question

I know no one does GUI by code now, but it is for studying purposes so..!

3

There are 3 answers

0
PeaceIsPearl On

It is good to use JPanels, create a Jpanel and add your elements( JLabel, JText) into it.

0
Krzysztof Cichocki On

You don't need to use Containers. Preferably use WindowBuilder to construct some GUI in eclipse, or Matisse in Netbeans, they are pretty good WYSWIG UI design tool. Try them. It would also be good for you to read some books about UI programming, especially about Swing if you plan to use it. I can advice the book "Filthy Rich Clients: Developing Animated and Graphical Effects for Desktop Java Applications"

2
Brian Knoblauch On

Sometimes it's nice to segment your stuff into containers for manageability. It's not required though. Other than dynamic components, very little GUI building is done in code anymore. As recommended by others, use a GUI builder for your primary work. If necessary you can drop into code, but I find that to be a pretty uncommon need.