File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Make Line Graph scrollable in a JScrollPane? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Make Line Graph scrollable in a JScrollPane?" Watch "Make Line Graph scrollable in a JScrollPane?" New topic
Author

Make Line Graph scrollable in a JScrollPane?

Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
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?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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!
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
GraphCanvas extends JPanel

I've been looking up Double Buffering on Google and it seems as though it is already being used with Swing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Make Line Graph scrollable in a JScrollPane?