Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GUI Scrollbar and file browser

 
Eric Crockett
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get some GUI's working. I am currently using a JFrame and displaying an image. If the image is too big for the screen though, some of it gets cut off. I want to add a scrollbar (vertical for right now) so I can scroll down and see the rest of the pic. This is what I have so far:


class Picture extends JPanel
{
JFrame frame;
static Image image;

public void display()
{
///////////////////CODE////////////////////////LEFT//////////OUT////////////
Picture display = new Picture();

frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.CENTER, display);
frame.setSize(width,height);
frame.setVisible(true);
}



I am wondering if someone can explain how to use the scrollbar constructor(what all the int values do) and how to get it displayed on the side of the JFrame.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing often winds up with many containers nested in each other. Look into putting a ScrollPane on the content pane, then another JPanel on top of that to hold your screen. YOu can set ScrollPane properties to have vertical and/or horizontal scroll bars all the time or only when needed.

Warning: I don't do a lot of Swing so that may not be exactly the order to lay things together. Snoop around the JavaDoc or google for "scrollpane example" and I bet you'll find your way!
 
Lionel Badiou
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Eric,

You may find explanation and sample code for scrolling large picture at How to use scrollpane


Best regards,
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic