• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Make Line Graph scrollable in a JScrollPane?

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GraphCanvas extends JPanel

I've been looking up Double Buffering on Google and it seems as though it is already being used with Swing.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic