A Java text area with a background image (watermark image)

Java image FAQ: How can I place an image in the background of a Java text component, such as creating a background image for a JTextArea, JTextPane, or JEditorPane? (i.e., How do I create a Java background or watermark image.)

Creating a watermark image in Java

A few days ago I ran across a website that used an image in the background of their HTML textarea, and I was reminded that a long time ago I did something similar with Java, letting use an image as the background for a Java text component, such as a JTextArea, JTextPane, JEditorPane, and probably even a JTextField. This is great for relieving the boredom of looking at a plain text editing area.

To give you an idea of what this looks like, I created a simple Java background image example, and here's an image of what my Java application looks like when it's running:

A Java text area with a background image (watermark image)

As you can see, the Java text area background image I'm using is subtle, but it has a nice effect; I no longer have to look at a plain white background (or other color) while using my text editor.

Java text area background image source code

Here’s the complete source code for my Java text area background image example:

import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import javax.swing.*;

 * A Java text area (JTextArea) with a background image.
 * You can use this class to add a watermark image to a Java text area,
 * or any other background image you like.
 * Alvin Alexander, http://alvinalexander.com
public class JavaTextAreaWithBackgroundImage extends JTextArea
  private final BufferedImage bufferedImage;
  private final TexturePaint texturePaint;

   * Supply an image file as an input field.
   * TODO - It would be a good idea to accept an image url as a separate constructor.
  public JavaTextAreaWithBackgroundImage(File file) throws IOException
    bufferedImage = ImageIO.read(file);
    Rectangle rect = new Rectangle(0, 0, bufferedImage.getWidth(null), bufferedImage.getHeight(null));
    texturePaint = new TexturePaint(bufferedImage, rect);

   * Override the painComponent method to do our image drawing.
  public void paintComponent(Graphics g)
    Graphics2D g2 = (Graphics2D) g;
    g.fillRect(0, 0, getWidth(), getHeight());

  public static void main(String[] args) throws Exception
    final JFrame frame = new JFrame("Java TextArea with Background Image");
    final JavaTextAreaWithBackgroundImage textArea = new JavaTextAreaWithBackgroundImage(new File("/Users/al/Desktop/text-area-background.jpg"));
    final JScrollPane scrollPane = new JScrollPane(textArea);

    SwingUtilities.invokeLater(new Runnable()
      public void run()
        scrollPane.setPreferredSize(new Dimension(360,225));
        textArea.setText("Some sample text here ...");

As you can see from looking at that code, you can place any image in the background of the JTextArea that you like. As noted in the code, in a more robust program, the constructor should also accept an image URL, so you can access images in a variety of ways.

I hope this Java text area background image example is helpful.