This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextArea image

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49782
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use setViewPortView, but add the text area directly to the JViewPort.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic