When making a graphics canvas in Java, which would be better to extend? Should you extend JPanel or Canvas? Are there any performance considerations?
Asked
Active
Viewed 4,916 times
1 Answers
2
Unless you need to position other components within the custom rendered area, sub-classing a JComponent is often all that is necessary (JPanel provides nothing more that is especially useful).
Mixing Swing with AWT
BTW - be especially wary of mixing Swing with AWT. It generally causes rendering problems for Swing floating GUI elements. Java 7 promises to provide functionality to seamlessly mix Swing and AWT based components.
E.G.
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 of the dialog using Java 6, when the JComboBox is expanded.

We can see the top of Apples, but the rest of the list is missing.
Andrew Thompson
- 168,117
- 40
- 217
- 433