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");
 frame.add(this);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(300,65);
 frame.setLocationRelativeTo(null);
 frame.setResizable(false);
 frame.setVisible(true);
}

public void paint(Graphics g)
{
 setOpaque(false);

 //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();
 g.drawImage(i,0,0,this);
 super.paint(g);
}

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

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!

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.

This article has been dead for over six months. Start a new discussion instead.