Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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: 72445
315
  • 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: 22120
114
Eclipse IDE Spring VI Editor 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: 22120
114
Eclipse IDE Spring VI Editor 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.
 
I was her plaything! And so was this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic