• Post Reply Bookmark Topic Watch Topic
  • New Topic

What's difference between JPanel and JComponent?  RSS feed

 
Napoleon Chu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should I use JPanel? What's the difference between JPanel and JComponent?

In Java Tutorial, the definition of JPanel is a ligthweight component as a general-purpose
container.

But JComponent is a more generic component to be used.

It inherits the java.awt.Container, and does a base-class for all Swing components except
the top-level container.That means when I need a top-level container, I'll choose JFrame but
not JPanel or JComponent. Because they both are not a top-level container.

1.As if I need a content pane to join the Container, then draw something or add more components
to this pane, JComponent could be.

2.If I want to get an UI from the component, JComponent could be.

I am confused about what the difference between JPanel and JComponent.
When should I use the one of them ?

Have someone could help to resolve it? And thanks a lot.
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the Java API Documentation for JComponent you'll see that it is declared as:

It is abstract so you can't create an instance of it. It serves only as a superclass for other classes.
JPanel, on the other hand, is a concrete class (meaning we can create instances of it) and is very useful for grouping components together.
 
Napoleon Chu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohoh.........I miss it....

Thanks a lot!

And...even I can add a JPanel into a JPanel?

Is that a good solution?
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's common to use JPanels to group components so yes, you will see a JPanel of buttons and a JPanel of display items added to a JPanel to make a calculator widget and add that to a JPanel point-of-sale interface and so on.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JPanel is a standard swing component implementation designed so it can be used directly without extension, so look and feel implementations will generally mess around with its settings as they would a JList or a JButton. For example most look and feels will set a JPanel to be opaque by default while a custom JComponent will not be opaque unless you set it manually. The BasicPanelUI can also install default settings for the JPanel font, foreground and background colors, and border.

Generally if you want to draw something manually you'll extend JComponent and override the paintComponent() method. When people extend JPanel they rarely override any JPanel methods. You'll see code that looks like this:



which is really the same as



The only advantage of using inheritence is the ability to save a few keystrokes. Compare this to a typical JComponent implementation:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!