skip super.paintComponent()

M

Martijn Mulder

Most texts urge me to call super.paintComponent() in the derived class. Is
there a compelling reason for that or can I rely on the proper execution of
the following class hierarchy?


class MyFirstComponent extends javax.swing.JComponent
{
public void paintComponent(java.awt.Graphics a)
{
super.paintComponent(a);
}
}

class MySecondComponent extends MyFirstComponent
{
public void paintComponent(java.awt.Graphics a)
{
// super.paintComponent(a);
// skipping super.paintComponent() here.
// Is that safe?
}
}
 
J

John C. Bollinger

Martijn said:
Most texts urge me to call super.paintComponent() in the derived class. Is
there a compelling reason for that or can I rely on the proper execution of
the following class hierarchy?


class MyFirstComponent extends javax.swing.JComponent
{
public void paintComponent(java.awt.Graphics a)
{
super.paintComponent(a);
}
}

class MySecondComponent extends MyFirstComponent
{
public void paintComponent(java.awt.Graphics a)
{
// super.paintComponent(a);
// skipping super.paintComponent() here.
// Is that safe?
}
}

The API docs are a primary reference that you should consult regarding
questions such as this. (I.e. those that bear on class / method
contracts.) In this case the API docs of JComponent note: "Further, if
you do not invoker super's implementation you must honor the opaque
property, that is if this component is opaque, you must completely fill
in the background in a non-opaque color. If you do not honor the opaque
property you will likely see visual artifacts." I take this to mean
that no, it is not imperative that you invoke super.paintComponent(), at
least when the superclass is JComponent. You should consider, however,
that paintComponent() may perform some work that you really want to have
done, especially for superclasses other than JComponent itself. Unless
you know *exactly* why you want to avoid invoking the superclass's
version, and exactly what the effect will be, my advice would be to do
as the texts tell you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top