• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

slight problem using JScrollPane

 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have an app that resizes the JFrame to fit the size of the image displayed in a JPanel. i decided to add a JScrollPane so it can display images that are larger than the screen(it cropped them). it works but the frame is a little too big and you cant see the horizontal scrollbar and only half of the vertical one. if i click the maximize button in the title bar, it displays correctly. how to correct this?

noticed i forgot to call super.paintComponent(g) but it worked anyway.
anyway adding that line had no effect.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you us an SSCCE?
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure. the entire program is very short.
i know. all my variables can be private. i changed that.
sorry if its a little hard to read, i'm still mixing tabs and spaces.
 
Rob Camick
Ranch Hand
Posts: 2564
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what pack() is doing when the preferred size is greater than the screen size, but on my system the horizontal/vertical sizes where both 12 pixels greater than the max.

I used the following to customize the size:



Also, there is no need to use a custom panel to display the image. I just added a JLabel to the scrollpane and then used:

 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. thanks Rob. the first code you posted looks like what i need. i used pack because sometimes the image is small. your second code is interesting as well.

that fixed it thanks
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI for anyone who cares. i tried Rob's suggestion about not needing to call repaint(). it worked at first(when files were different size). but when i opened files the same size(my documents/my pictures/sample pictures), pack() didn't call repaint(). in fact pack didn't get called because the size was the same. so i needed to call repaint() after all.
 
Rob Camick
Ranch Hand
Posts: 2564
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a JLabel and reset the Icon there is no need to call repaint() because the setIcon() method calls repaint().
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'll try that next since it will be less code than using custom JPanel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic