Java Canvas or JPanel

When creating a graphic canvas in Java, which is better to extend? Should you extend JPanel or Canvas? Are there any performance considerations?

+4
source share
1 answer

If you do not need to position other components in the field of custom visualization, then subclassing JComponent often requires all that is needed ( JPanel does not provide anything more useful).


Blend Swing with AWT

BTW - Use extreme caution when mixing Swing with AWT. This usually causes processing problems for Swing floating GUI elements. Java 7 promises to provide functionality for seamlessly mixing Swing and AWT components.

eg.

 import java.awt.*; import javax.swing.*; class MixSwingAwt { public static void main(String[] args) { JPanel p = new JPanel(new BorderLayout(10,10)); String[] fruit = {"Apples", "Oranges", "Pears"}; JComboBox fruitChoice = new JComboBox(fruit); p.add(fruitChoice, BorderLayout.NORTH); p.add(new TextArea(10,20)); JOptionPane.showMessageDialog(null, p); } } 

Screenshot

Screenshot using Java 6 with JComboBox extension. enter image description here

We see the top of the Apples , but the rest of the list is missing.

+2
source

All Articles