Adding a watermark to a JTextArea
Is they 开发者_StackOverflowa way of adding a watermark to a JTextArea?
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)
.
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
精彩评论