Set JTextArea background image

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

***********************************************************************
COMPLETE SOURCE CODE FOR : AddImageBackgroundInJTextArea.java
***********************************************************************


/**
*AddImageBackgroundInJTextArea will extends JTextArea class.
*So it also inherit all method from JTextArea
*It mean, we can use it directly.For example, setOpaque(false)..
**/
import javax.swing.*;

import java.awt.*;

public class AddImageBackgroundInJTextArea extends JTextArea
{
public AddImageBackgroundInJTextArea()
{
//Create a window using JFrame with title ( Add image background in JTextArea )
JFrame frame=new JFrame("Add image background in JTextArea");

//Add JTextArea into JFrame
frame.add(this);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(500,500);

//Make JFrame center
frame.setLocationRelativeTo(null);

//Make JFrame visible
frame.setVisible(true);
}

//Override JTextArea paint method
//It enable us to paint JTextArea background image
public void paint(Graphics g)
{
//Make JTextArea transparent
setOpaque(false);

//Make JTextArea line wrap
setLineWrap(true);

//Make JTextArea word wrap
setWrapStyleWord(true);

//Get image that we use as JTextArea background
ImageIcon ii=new ImageIcon("textAreaBackground.jpg");
Image i=ii.getImage();

//Draw JTextArea background image
g.drawImage(i,0,0,null,this);

//Call super.paint. If we don't do this...We can't see JTextArea
super.paint(g);
}

public static void main(String[]args)
{
AddImageBackgroundInJTextArea aibijta=new AddImageBackgroundInJTextArea();
}
}


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



textAreaBackground.jpg

RELAXING NATURE VIDEO