• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JTextArea image

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80212
423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic