I dont understand why we are using super.paint() in this code. help me to figure out why?

public class SetJTextFieldBackgroundImage extends JTextField
public SetJTextFieldBackgroundImage()
 JFrame frame=new JFrame("Set JTextField background image");

public void paint(Graphics g)

 //Image that will be set as JTextField background
 //I suggest, image that will be use is blur...
 //If image is locate at other location, you must put it's full location address.
 //For example : C:\\Documents and Settings\\evergreen\\Desktop\\textFieldBackgroundImage.jpg
 ImageIcon ii=new ImageIcon("textFieldBackgroundImage.jpg");
 Image i=ii.getImage();

public static void main(String[]args)
 SetJTextFieldBackgroundImage sjtfbi=new SetJTextFieldBackgroundImage();

Recommended Answers

All 4 Replies

your paint method is overriding the paint method of the super class (JTextField) because you need to add a bit of functionality.

just in order for it to work, you'll also need the original code to run, so using
super.paint(g); you call the method that's in your superclass and tell it to run.
basically, by adding that line, you are just "copy-pasting" all the code and functionality of the original method into your own.

The inherited paint method from JTextField is where the actual text is painted into the field. Because you have setOpaque(false) the inherited paint will not attempt to draw any background, so it will just paint the text over the image you painted in your code.
If you didn't call super.paint then nobody would draw the text, which pretty much limits its usefulness to highy-secure password fields!

i didnt understand the last two lines sir

Sorry - it was a joke. If you don't call super.paint() then the text will never be painted. I joked that this could be a good idea for password fields, so nobody could see your password. Never mind. J.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.