Question

Is they a way of adding a watermark to a JTextArea?

Was it helpful?

Solution

I suspect that you'd need to subclass JTextArea and override the paintComponent() method, drawing your background image first and calling super.paintComponent() to render the text:

public void paintComponent (Graphics g) {
    g.drawImage(watermark, 0, 0, this);
    super.paintComponent(g);
}

edit: as pointed out by camickr, a JTextArea is opaque, so your subclass will need to change this by calling setOpaque(false).

OTHER TIPS

I doubt the suggestion given above will work. A JTextArea is opaque, so the text will paint over top of the image. So at the very least you will need to make the text area non-opague and you will then need to play with the background colors of the viewport and/or scrollpane.

If you want a reusable solution try creating an ImageBorder. The order of painting is:

a) paintComponent

b) paintBorder

c) paintChildren

So if you add the border to the text area it will paint on top of the text in a fixed location.

Or if you add the border to the viewport it will paint below the text is a floating location.

You may also consider using JXLayer which can create quite complex visual effects

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top