Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Set JTextField background image (Java)

Complete source code below will show you, how to set image background in a JTextField. You can download image (textFieldBackgroundImage.jpg) that use in source code at below.

*******************************************************************
COMPLETE SOURCE CODE FOR : SetJTextFieldBackgroundImage.java
*******************************************************************
import javax.swing.*;

import java.awt.*;

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();
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************




textFieldBackgroundImage.jpg