I found that it’s somewhat tricky to override GUI components methods with Netbeans, because the IDE automatically generates the code needed for the component, and that code cannot be edited (it’s grayed out).
But there is a property, in the “Code” tab of the component properties called “Custom Creation Code”, that let us insert the creation code we need for that component.
For example. Create a new desktop application project and, using the design view, drop a new JPanel inside the main panel. If you inspect the source code that Netbeans has generated, you can see the declaration:
private javax.swing.JPanel jPanel1;
And the initialization for that JPanel:
jPanel1 = new javax.swing.JPanel();
Note that the code after the “=” is called “Creation Code”.
Now, open the properties menu of the JPanel you have just dropped before and click on the “code” tab. Click on the “Custom Creation Code” property.

This property allow us to insert whatever code we need for the creation of the component.
Today we just want to override paint() so we insert this code:
new javax.swing.JPanel()
{
public void paint(Graphics g)
{
super.paint(g);
ourCustomPaintingMethod(g);
}
};
If you check the generated code again you can see that Netbeans has changed the creation code:
jPanel1 = new javax.swing.JPanel()
{
public void paint(Graphics g)
{
super.paint(g);
ourCustomPaintingMethod(g);
}
};
This way we can override any GUI component method we want 😉