Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Scrollable JPanel Drawing in JScrollPane  RSS feed

 
Di Shoe
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I drew graphs in a class that extened JPanel and drew in the paintComponent() method. Then in my applet, I instantiated that class and put it in a JScrollPane. However, the JScrollPane can't realized how big the drawing is. Instead of scrolling when the drawing is really big( I made it bigger then the available width and height), the scrollPane just let it run off. Are there any work arounds or am I fundamentally wrong....??
Thank you
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to override getPreferredSize() to return a value that makes sense for your component. And you would probably have to call validate() on the parent component whenever your size changed so they could update correctly.
 
Di Shoe
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Nate. It worked just as you described. However, I wonder why do we have to overwrite the getPreferredSize() method of the JPanel object? when I didn't do that and just setPreferredSize(new Dimension(xx,yy)) for the object, it didn't work. But once I do that, shouldn't the default getPreferredSize() pick up the new Dimension?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!