I know this question is quite old but I stumbled upon it while looking for something else and thought I would add my two cents for others who come across it.
The CAUSE: The cause of the OPs reported issue is exactly as he postulates... i.e. the behavior in the super.paintComponent(g)
effectively paints over his custom painting code.
However... there is nothing really wrong with the code that he posted. It does/will work and I must respecfully disagree with the DanD's suggested answer. Unfortunately, the OP does not include more example code so his exact issue can only be speculated.
Huh? you ask. Then what's going on?
The FIX: Quite simply, if you want to customize the appearance of JTextField using this approach, then you must effectively tell the superclass not to paint over your custom painting. This is accomplished by setting the following property values on your component:
text.setOpaque(false);
text.setBorder(null);
In this manner, you can leave all code as-is and you are effectively telling the original paintComponent() method not to draw a background color nor draw a border.
I offer the following SSCCE as an example and solution.
Note the following:
I created a static boolean 'FIXIT'. Hopefully, it goes without saying that you would not include this in "production" code; it is being used within the example to allow you to observe both behaviors. Simply toggle its value to see "BEFORE" and "AFTER".
More importantly (IMO), notice that I set the mentioned property values OUTSIDE of the custom code class. I did this to more align with the way that the OP asked the question and this approach would not require a change to his custom class. However, as the comment in the code states, a more robust solution would set these values in the custom class constructor to ensure their correctness.
I also offer a slightly more visually appealing paint method. While it is almost identical to the original, see the comments for the tweaks made. This is, of course, just my opinion but it is offered as an example nonetheless.
package misc;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
/**
* An SSCCE in response to the following StackOverflow question:
* https://stackoverflow.com/questions/13109638/my-custom-jtextfield-covered-by-super-paintcomponentg-method
*
* @author kansasSamurai
*
*/
@SuppressWarnings("serial")
public class CustomTextField extends JTextField {
private static boolean FIXIT = true;
public static void main(String args[]) {
SwingUtilities.invokeLater( new Runnable() {
@Override public void run() { createAndShowGUI(); }
} );
}
protected static void createAndShowGUI() {
JFrame frame = new JFrame("SSCCE - CustomTextField");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridBagLayout());
CustomTextField text = new CustomTextField();
text.setText(" Ipsum Lorem ");
if (FIXIT) {
// These two lines are NECESSARY...
text.setOpaque(false);
text.setBorder(null);
// or!!! ... they could/should be added to the constructor(s) of your custom class
// These two lines are OPTIONAL
text.setForeground(Color.white);
text.setCaretColor(Color.white);
}
frame.getContentPane().add(text);
frame.pack();
frame.setSize(330, 100); // these dimensions are "arbitrary"
frame.setLocationRelativeTo(null); // centers the frame on screen
frame.setVisible(true);
}
@Override
protected void paintComponent(Graphics g) {
if (FIXIT) {
this.betterPaint(g);
return;
}
final Graphics2D gd = (Graphics2D) g.create();
gd.setPaint(new GradientPaint(0, 0, Color.BLUE, getWidth(), 0, Color.BLACK));
gd.fillRoundRect(0, 0, getWidth(), getHeight(), getWidth() / 2, getHeight() / 2);
gd.dispose();
super.paintComponent(g);
}
protected void betterPaint(Graphics g) {
final Graphics2D gd = (Graphics2D) g.create();
// Improve appearance by enabling antialiasing
gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gd.setPaint(new GradientPaint(0, 0, Color.BLUE, getWidth(), 0, Color.BLACK));
// Fully rounded ends (not strictly required to fixit... just an example)
gd.fillRoundRect(0, 0, getWidth(), getHeight(), getHeight(), getHeight());
gd.dispose();
super.paintComponent(g);
}
}