Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JPanel in a JScrollPane in a JApplet: No Scrollbars?

796262May 15 2009 — edited May 15 2009
Hi all,

I'm trying to create a JApplet that contains a large, scrollable JPanel. Eventually the JPanel will contain custom drawn items, but the first step is to just make the thing scrollable. However, the scroll bars are not activated unless I set them to always visible, and even then the actual bars that allow scrolling are not present. This is the first time I'm trying to write an applet, so I'm not sure whether the problem is with the JPanel, the JScrollPane, or the JApplet.

Strangely enough, if I change the width and height of the applet in the html calls, the scroll bars do activate. Why is that? Since I'm adding the JPanel to the JScrollPane, and the JScrollPane to the JApplet, why would the JScrollPane seem to scroll the whole applet and not just the JPanel? I know I'm misunderstanding something here, but I'm not sure what.

Anyway, here's a compilable example that demonstrates what I'm talking about:
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.Container;
import java.awt.Color;


public class TestApplet extends JApplet{

   public void init(){

      JPanel panel = new JPanel();
      panel.setSize(1000,1000);
      panel.setBackground(Color.GREEN);
      
      JScrollPane scrollPane = new JScrollPane(panel);

      Container pane = this.getContentPane();
      pane.add(scrollPane);

   }

   public void start(){}
   public void stop(){}
   public void destory(){}
}
And here's the applet tag I'm using to launch it in a browser:
<APPLET CODE="TestApplet.class" WIDTH=100 HEIGHT=100>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 12 2009
Added on May 15 2009
9 comments
815 views