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.
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.
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.
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!