Hey guys,

How do you tell which type of GUI should you use ? I'm making a swing application but everytime I look at a Java example / tutorial they tend to use different ones for each example.

Is there a reason? I'm guessing so which leads me to which one should you use and why?

JFrame is a top-level component and as such can stand on it's own as a window. JPanel is a lightweight container in which other components can be placed. JPanel cannot be displayed by itself as a top-level window.

If you have a window with a single main area, using JFrame by itself can suffice. If you wish to subdivide that window into smaller component groups, you can place these components in a JPanel to manage them easier. JPanel can also be placed into components such as JScrollPane, JTabbedPane, etc to act as subforms within a window.

It's often convenient to have form classes subclass JPanel and then place them into other containers as needed. This allows you greater freedom to organize such forms or component groupings in any manner you choose.

So would it be ideal to get a JFrame first and then start using JPanel for the components?

Yes. JFrame is the application window (or any other window in the application that's not a JDialog.
You build up substructures to be contained in that window on JPanels and place those in the JFrame (or in other JPanels which get placed eventually in the JFrame).

Experiment, layer components, combine different layout managers to get the effects you want.

This article has been dead for over six months. Start a new discussion instead.