0

I have a class that extends JFrame and pops up a window (frame). I successfully added a jpanel and a jscrollpane that uses that panel.

I have a Jbutton on the jpanel that uses actionlistener and will call repaint (or paintComponents, whichever you think is better) every time I click the button.

The problem is, what gets painted will get erased when I scroll over the painted material. The JButton stays and doesn't get redrawn, but the stuff that gets painted gets erased.

So my question is: How do I paint outside of the JViewport on a JScrollpane's panel?

I have been trying in vain, experimenting with the Graphics class methods such as Graphics.setClip() and JViewport.setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE)...

the setClip() method in fact does the opposite of what I want to do. It limits the paintable area of the chosen component, when in fact I want to expand the area that gets painted - I want to paint (and keep painted) the area outside of the viewport.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 1
    This painting should be done automatically and should not require any special code on your part, and because it's not, it sounds as if something is very wrong with your program, with how you're doing your painting. Perhaps you're using `getGraphics()` to get a Graphics object, perhaps you've got program logic in a `paint()` or `paintComponent()` method or some other similarly bad error -- don't know without code. Have you read the Swing graphics tutorials before doing this? If not, do yourself a favor and see how it's supposed to be done. Consider showing us your graphics code. – Hovercraft Full Of Eels Feb 21 '12 at 18:01
  • 1
    By the way, you'll almost never call `paint(...)` or `paintComponent(...)` directly. – Hovercraft Full Of Eels Feb 21 '12 at 18:05
  • You might see if [`ScrollPanePaint`](http://stackoverflow.com/a/3518047/230513) offers any insight. – trashgod Feb 21 '12 at 18:21
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 21 '12 at 18:32
  • Hi everyone, thanks for the comments, just so you know, over time the only solution I found to circumvent this problem was to draw on some jpanels first, then add them to the jpanel inside the jscrollpane. – Alexander Mills Jan 30 '13 at 19:55
  • What I would love to see from java at some point is some sort of feature where you can draw/paint WITH Objects. So instead of drawLine, drawShape, etc. It would be, addLine, addShape, etc. Maybe I missing something, but I have done so much googling on the subject that I don't think Java has some sort of "drawing with objects" feature...? – Alexander Mills Jan 30 '13 at 19:56

0 Answers0