Scollbar

How to use the ScrollBar on a Applet?
How to use the ScrollBar on a Panel?
hongvu547Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
SmillyConnect With a Mentor Commented:
Hi hongvu547,
Here is a example on how to use a scrollbar, and it
works both in a Applet and a Panel.

Hope this help....

 import java.awt.Panel;
 import java.awt.Scrollbar;
 import java.awt.Event;
 import java.awt.event.*;
 import java.util.EventListener;

public class TstScroll extends Panel implements MouseListener {
{
    Scrollbar hSb, vSb;
    int xpos,ypos;
    public TstScroll()
    {
      hSb = new Scrollbar(Scrollbar.HORIZONTAL);
      vSb = new Scrollbar(Scrollbar.VERTICAL);
      add(hSb);
      add(vSb);
      hSb.setVisible(true);
      vSb.setVisible(true);
      hSb.setBackground(new Color(198,195,198));
      vSb.setBackground(new Color(198,195,198));
      hSb.setValues(0,1,0,100);
      vSb.setValues(0,1,0,100);
        vSb.setBounds(1,getSize().height-16,getSize().width-15,15);                  
        hSb.setBounds(getSize().width-16,1,15,getSize().height-15);                  
    }


      public boolean handleEvent(Event e)
      {
            switch (e.id) {
              case Event.SCROLL_LINE_UP:
              case Event.SCROLL_LINE_DOWN:
              case Event.SCROLL_PAGE_UP:
              case Event.SCROLL_PAGE_DOWN:
              case Event.SCROLL_ABSOLUTE:
                              if (e.target == hSb) {
                                    xpos = hSb.getValue();
                                    return true;
                              }      
                              else if (e.target == vSb) {
                                    ypos = vSb.getValue();
                                    return true;
                              }      
        }
        return super.handleEvent(e);
      }
}


                  

0
 
hongvu547Author Commented:
Edited text of question.
0
 
heyhey_Commented:
it depends on why you want to use it :)
simple add it to the container (panel / applet) add AdjustmentListener and track all the events.
0
 
SmillyCommented:
I forgot remove mouselistener, it isn't part of your task..
So remove mouselistener.....

Smilly
0
All Courses

From novice to tech pro — start learning today.