Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to add scrollbar to a JPanel with Graphics

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to add scrollbar to a JPanel with Graphics

    I have a JInternalFrame. Inside this frame I have a JPanel which is a drawing panel.
    It implements the paint() method and draws something within the panel.
    I want to add a scrollbar for this JPanel so that I can scroll down and see different parts of what I draw. I use the following code within a method in JInternalFrame:


    jp = new DrawingPanel(); // DrawingPanel is a subclass of JPanel which implements paint();
    JScrollPane scroller = new JScrollPane(jp);
    scroller.setVerticalScrollBarPolicy(ScrollPaneCons tants.VERTICAL_SCROLLBAR_ALWAYS);
    add(scroller);
    add(jp);

    The program can compile. and I can see the graphics inside the DrawingPanel.
    But the problem is that the scrollbar doesn't even appear.
    How can I fix this problem with the least modification to the whole structure of the program?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to add scrollbar to a JPanel with Graphics

    First, you should override the paintComponent method rather than the paint method (if that is truly what you've done). Second, set the preferred size of the JPanel you've added to the JScrollPane (before the JScrollPane is visible - if added after then call revalidate on the added JPanel) - this should let the JScrollPane know the size of the component it is trying to display.

Similar Threads

  1. Help about Graphics
    By mamech in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 9th, 2010, 03:20 PM
  2. graphics in job?
    By SweetyStacey in forum The Cafe
    Replies: 10
    Last Post: May 3rd, 2010, 03:29 PM
  3. problem in getting scrollbar in Jlist
    By sandeepben in forum AWT / Java Swing
    Replies: 1
    Last Post: March 27th, 2010, 03:51 AM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM