Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

New Image in JScrollPane

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to create an application that can display different images in a single frame. The main frame contains a JScrollPane that contains a JPanel. Initally there is no image on the JPanel and there are no scrollbars on the scroll pane. When I add a large image to the JPanel the scrollbars don't appear on the scroll pane. What function do I need to call for the scroll bars to appear? I tried revalidate() and it did not work.
Thanks,
SK
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You add the scrollpane around the JPanel using
JScrollPane scroll = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Or if you need the scrollbars to be present always, change the 2nd and 3rd parameters to
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS and
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
Hope this helps.
Ashwin.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure how you add this image to the JPanel, but I am supposing that you just paint this image on top of this panel. One way I think you can try is to set the preferred size and minimum size dynamically according to the image size you loaded, then call revalidate, and that should force the JScrollPane to scroll.
code sample:
public Dimension getMinimumSize()
{
return new Dimension(image.getWidth(panel), image.getHeight(panel));
}
in your load function, then call revalidate().
Hope this helps.

Originally posted by Skantha Kandiah:
Hi,
I am trying to create an application that can display different images in a single frame. The main frame contains a JScrollPane that contains a JPanel. Initally there is no image on the JPanel and there are no scrollbars on the scroll pane. When I add a large image to the JPanel the scrollbars don't appear on the scroll pane. What function do I need to call for the scroll bars to appear? I tried revalidate() and it did not work.
Thanks,
SK


 
Skantha Kandiah
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help.
Adding the getPreferredSize() method and calling the revalidate() function fixed my problem. I had to use MediaTracker to wait for the image to finish loading before calling repaint() and revalidate().
But this seems like an awful lot of work just to display an image on a JPanel. I thought some of the work will be handled automatically, like revalidation when a component changes.
Isn't there a simple way to add and remove images from a panel?
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic