aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTextArea image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextArea image" Watch "JTextArea image" New topic
Author

JTextArea image

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, I'm trying to put an Image in a JTextArea, I've managed to do so using:



But the problem is, my JTextArea is in a ScrollPane, so when I scroll down, the image scrolls with the text.
The effect I would like is the image to not move, while the text scrolls over it.

I think this is possible, but I don't know how to do it.
I considered adding the image to ScrollPane as I have above, then making the JTextArea transparent, but I couldn't get it to work.

Any thought? Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
You can't add images to scroll panes (at least I think you can't). You have to add Components to scroll panes, then stick the image on the component. Then you might make it transparent.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
I've tried a few things, starting with a JPanel, with an image, then a JScrollPane on top of that, which contains the JTextArea.
The image shows, but the also covers the text, so its not quite right, and I'm not really sure what I'm doing.

Any help is appreciated, thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
make the textArea opaque(false)
use the image/paintComponent method for a JViewport
set this as the viewport of the scrollpane
set the viewportView as the textArea
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks, but I'm not quite sure how I have to set it up, I haven't used ViewPort before.

I basically have this code

But it doesnt do anything, any ideas?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Don't use setViewPortView, but add the text area directly to the JViewPort.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks, its slightly better, I can see the text and its on a grey background instead of white, but there is no image yet.

My code is:


Thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Excellent, thanks.

I got it working now, I don't fully understand it yet, but I can play around with it.

Thanks again.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

colin shuker wrote:

super.paint(g) (and super.paintComponent(g)) clears all previous drawing, then starts all over. So you should move your drawImage call to after the super.paint(g). Also, do NOT dispose this Graphics object - it's still needed for drawing the rest of the GUI. The Swing API will dispose it when needed.

Finally, override paintComponent instead of paint like Michael has shown. Just keep the method protected.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextArea image