Scrolling a custom Component (e.g. JPanel) with overridden paint(Graphic g)

Hi.

I’m creating an application for modelling advanced electrical systems in a house. Until now I have focused on the custom canvas using Java2D to draw my model. I can move the model components around, draw lines between them, and so on.

But now I want to implement a JScrollPane to be able to scroll a large model. I’m currently using a custom JPanel with a complete override of the paint(Graphic g) method.

Screen-shot of what I want to scroll:

http://pchome.grm.hia.no/~aalbre99/ScreenShot.png

Just adding my custom JPanel to a JScrollPane will obviously not work, since the paint(Graphic g) method for the JPanel would not be used any more, since the JScrollPane now has to analyze which components inside the container (JPanel) to paint.

So my question is therefore: How do you scroll a custom Component (e.g. JPanel) where the paint(Graphic g) method is totally overridden.

I believe the I have to paint on a JViewport instructing the JScrollPane my self, but how? Or is there another solution to the problem?

Thanks in advance for any suggestions.

Aleksander.

[1143 byte] By [Alex00001a] at [2007-10-2 9:38:12]
# 1

> I抦 currently using a custom JPanel with a complete override of the paint(Graphic g) method.

Althought this isn't your problem, you should be overriding the paintComponent(..) method, not the paint(..) method.

> But now I want to implement a JScrollPane to be able to scroll a large model.

When you create a custom component to do custom painting then you are responsible for determining the preferredSize of the component. So, you need to override the getPreferredSize(...) method to return the preferredSize of your component. Then scrolling will happen automatically when the component is added to a scrollPane.

camickra at 2007-7-16 23:44:19 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks for the help :-) Works like a charm !However, one question though:What is the difference between the paint() and the paintComponent() method in a Component? In other words; why override the paintComponent() and not the paint() method?Aleksander.
Alex00001a at 2007-7-16 23:44:19 > top of Java-index,Desktop,Core GUI APIs...
# 3
> why override the paintComponent() and not the paint() method?Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/14painting/concepts.html]How Painting Works[/url].
camickra at 2007-7-16 23:44:19 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thanks for all the help camickr.
Alex00001a at 2007-7-16 23:44:19 > top of Java-index,Desktop,Core GUI APIs...