Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2005

Tip: Looking for answers? Try searching our database.

how to make a scroll pane in a JApplet

Thread view: 
thewhoracle - 31 May 2005 17:39 GMT
I am using gridbaglayout and have a 800x600 Japplet.  i want it to stay
800x600 but be able to scroll down because i have too many fields to
fit on 600.  I tried using the examples java.sun has it won't work.
can someone point to me a very simple example of scrollpane or
scrollbar usage in a JApplet or tell me an easy way to do this?
Thomas Fritsch - 31 May 2005 18:39 GMT
thewhoracle schrieb:

> I am using gridbaglayout and have a 800x600 Japplet.  i want it to stay
> 800x600 but be able to scroll down because i have too many fields to
> fit on 600.  I tried using the examples java.sun has it won't work.
> can someone point to me a very simple example of scrollpane or
> scrollbar usage in a JApplet or tell me an easy way to do this?

Try the following in your JApplet:
    public void init()
    {
        JComponent yourLargeComponent = ....;
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.getViewport().add(largeComponent);
        this.getContentPane().add(scrollPane);
    }
Note that you must not add directly to the
JScrollPane/JApplet but to their viewport/contentpane.

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Roland - 31 May 2005 18:53 GMT
> I am using gridbaglayout and have a 800x600 Japplet.  i want it to stay
> 800x600 but be able to scroll down because i have too many fields to
> fit on 600.  I tried using the examples java.sun has it won't work.
> can someone point to me a very simple example of scrollpane or
> scrollbar usage in a JApplet or tell me an easy way to do this?

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ScrollPanel extends JApplet {

   public ScrollPanel() {
      createGUI();
   }

   private void createGUI() {
      JPanel gridpane = new JPanel();
      gridpane.setLayout(new GridBagLayout());

      // Add some components:
      for (int r = 0; r < 40; r++) {
         for (int c = 0; c < 3; c++) {
            JComponent comp = new JLabel("Label " + r + " " + c);
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(0, 6, 6, 0);
            gbc.gridx = c;
            gbc.gridy = r;
            gbc.weightx = 1.0;
            switch (c) {
            case 0:
               gbc.anchor = GridBagConstraints.WEST;
               gbc.insets.left = 0;
               break;
            default:
            case 1:
               //gbc.anchor = GridBagConstraints.CENTER;
               gbc.anchor = GridBagConstraints.WEST;
               break;
            case 2:
               //gbc.anchor = GridBagConstraints.EAST;
               gbc.anchor = GridBagConstraints.WEST;
               break;
            }
            gridpane.add(comp, gbc);
         }
      }

      JScrollPane scrollpane = new JScrollPane();
      scrollpane.getViewport().add(gridpane);

      JLabel statusbar = new JLabel("Status");
      statusbar.setBorder(BorderFactory.createLoweredBevelBorder());
      this.getContentPane().setLayout(new BorderLayout());
      this.getContentPane().add(new JButton("North"),
                                BorderLayout.NORTH);
      this.getContentPane().add(scrollpane, BorderLayout.CENTER);
      this.getContentPane().add(statusbar, BorderLayout.SOUTH);
   }

   public void destroy() {
      super.destroy();
   }

   public void init() {
      super.init();
   }

   public void start() {
      super.start();
   }

   public void stop() {
      super.stop();
   }
}

Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.