Java GUI Framework Question:

What is the equivalent of AWTs Canvas in Swing?

Tweet Share WhatsApp

Answer:

JPanel, if you want to have a "complete" component with a UI delegate which handles opaque settings (if paintComponent() is correctly overridden).

JComponent if you intend to always draw every pixel in the area of the component (and break the opaque attribute API contract). If you need to have your own special key and mouse processing, you might also want to start with JComponent and create your own UI delegate.

You could even start higher up in the inheritance chain. java.awt.Component is lightweight since Java 1.1. However, you will not get Swing additions like double-buffering.

If this is all Greek to you, use JPanel. And remember, if you use JComponent or JPanel, override paintComponent(), not paint().

Download Java GUI Framework PDF Read All 28 Java GUI Framework Questions
Previous QuestionNext Question
How to get the screen size? How Do I center a window on the screen?How to generate some charts / plots in Java?