Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot get JScrollPane to display working scrollbars

 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having quite a bit of difficulty getting a JScrollPane to display a BufferedImage with working scrollbars. I studied the Sun/Oracle tutorial and API to no avail.

I created a SSCCE to showcase my problem.

Here's the image I'm working with: Image Link





If I run the program with the default Scrollbar policy (*_SCROLLBAR_AS_NEEDED), no scrollbars display. Screen Capture

If I run the program with the Scrollbar policy of *_SCROLLBAR_ALWAYS, the scrollbars display but they're maximized to the width and height of the screen. Screen Capture

I read in other threads that it's important to set the preferred size of the JScrollPane. I tried that and it had no noticeable effect on the scroll bars.

Any advice would be greatly appreciated.

Bill
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tested your provided code. The scrollPane as is doesn't really show the scroll bars. If I add in scrollPane.setPreferredSize(new Dimension(x,y)) then works perfectly fine.

To find x and y, you should get it from the BufferedImage, rather than hard code a value for width and height.
 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm....

I added the line



I still don't see the scrollbars.
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right, I wouldn't expect that setting a preferredSize to the scroll pane would help here. I think K. Tsang meant that you should set a preferredSize to the ImagePanel. A better approach would be to override getPreferredSize() to return the size of the image.

But a simpler approach would be to use a JLabel with an ImageIcon, in a JScrolPane. The label's preferredSize will be computed based on the size of the icon.

Of course, that assumes that all you want to do is display an image with scrollbars.
 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:That's right, I wouldn't expect that setting a preferredSize to the scroll pane would help here. I think K. Tsang meant that you should set a preferredSize to the ImagePanel. A better approach would be to override getPreferredSize() to return the size of the image.


It worked! I overrode getPreferredSize in the ImagePanel class and it works like a charm!

Thank you Darryl and K. Tsang!
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked! I overrode getPreferredSize in the ImagePanel class and it works like a charm!


You missed the part about the easiest approach is to use a JLabel with an ImageIcon. Don't reinvent the wheel.
 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
You missed the part about the easiest approach is to use a JLabel with an ImageIcon. Don't reinvent the wheel.


Can an ImageIcon work with a TIF image? The API only mentions GIF, JPG, and PNG.
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Clar wrote:Can an ImageIcon work with a TIF image? The API only mentions GIF, JPG, and PNG.


What not try it and find out yourself? Other formats you can try are BMP and SVG. Unfortunately I can think of 2. Practically any image formats.
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not what ImageIcon 'works' with; if constructed with a String argument, that's limited to image types that can be loaded by java.awt.Toolkit. But ImageIcon also has a constructor (among others) that takes an Image parameter, and there are other ways to load an image.

If you have an image file that you can't load as a Java Image, you can't display the image anyways.
 
Hug your destiny! And hug this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!