Forums Register Login
Make Line Graph scrollable in a JScrollPane?
I've created a line graph from data read in from a file. This "line" is drawn by a Graphics2D object.

I tried just simply adding the drawing panel to a JScrollPane instead of the normal panel it was currently in. However, that did not display the scroll bars even though I have added points past the side of the panel which is currently visible.

I know I could implement some buttons or key listeners to manually redraw the graph as though it had scrolled, but the file I am reading is quite large, so reading/drawing it once is the best option as the values are not stored in memory... if possible.

Thanks in advance.
For a panel to be scrollable, it has to tell the JScrollPane what size it "wants" to be. If a JPanel has components in it, then it bases this desired size on what size those components want to be. If it's empty, then its preferred size is zero. So all you have to do is tell the JPanel you're drawing on what size you want it to be by calling setPreferredSize(); you'll then find that a JScrollPane will show scroll bars as needed.
Here is a sample of my default constructor.

Scroll is still not there. My line goes to 900 pixels to the right so I set the preferred size to be overkill as it will be going considerably larger.

Any other thoughts?
You add your canvas to the scroll pane, but then turn around and add it to the content pane, which removes it from the scroll pane. A component can only have one parent! You need to add the canvas to the scroll pane, and then add the scroll pane to the content pane.
Ahh, silly me.. you can see that I started without a JScrollPane.

However, now that I have changed the line to drawable.add(jsp);, nothing is shown on the canvas panel.

I have played around adding some .setVisible(true) to canvas, jsp, and drawable, but that did not seem to help.
Sorry, I found another mistake -- it's hard sometimes to spot wrong but valid-looking code!

You don't use "add()" to add things to a JScrollPane. A JScrollPane can only manage one component, and generally, you give that component to the JScrollPane as a constructor argument. I think there may be another way to add it at runtime but I've never actually done it -- and I know that just calling add() ain't it!

So change

JScrollPane jsp = new JScrollPane(canvas);

and Bob'd your uncle, I believe.
Thank you Ernest.

It is beginning to work now. Hmm, Java has a very poor scroll to a paint window. I remember seeing this before when using the AWT.LIST class as well.

Hmm, perhaps Java is not the best language to be writing this in for appearance sake.
Perhaps you can turn double-buffering on if it's off. What class does GraphCanvas extend?

I'm going to move this thread to Swing/AWT/SWT/JFace, which is where it belongs, anyway. See you over there!
GraphCanvas extends JPanel

I've been looking up Double Buffering on Google and it seems as though it is already being used with Swing.

This thread has been viewed 1993 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 15, 2018 08:38:18.