• Post Reply Bookmark Topic Watch Topic
  • New Topic

ScrollPane/ScrollBar  RSS feed

 
Rajesh Radh
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is there any method that gives me the "direction" (Positive /Negative) in which the ScrollBar is moving. I looked into BasicScrollBarUI class, there are two constants POSITIVE_SCROLL and NEGATIVE_SCROLL. But I need to know from an instance of a ScrollPane class, whether user clicked POSITIVE_SCROLL or NEGATIVE_SCROLL. Please help me.
Let me explain what I am trying to do. I have defined an array of length 12. This array contains values {"Jan", "Feb"� , "Dec"}. When my scroll-point is at "Dec", a POSITIVE_SCROLL must display "Jan" (I am trying for circular scrolling!). Similarly, When my scroll-point is at "Jan", a NEGATIVE_SCROLL must display "Dec". Does anyone know if there is a class that does this already?

Thanks,
 
Sanjay Bahrani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I think you should put the listener on the scrollbar i.e AdjustmentListener and use this method getValue() of AdjustmentEvent class.
I am giving you the example on applet, hope you will find it useful.....
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/** <applet code = "ScrollDemo.class" width = 300 height = 300>
</applet> */
public class ScrollDemo extends Applet
{
Scrollbar hsb;
Scrollbar vsb;
int v,h;
public void init(){
setLayout(new BorderLayout());
hsb = new Scrollbar(Scrollbar.HORIZONTAL,1,10,0,size().width);
vsb = new Scrollbar(Scrollbar.VERTICAL,1,10,0,size().height);
add("North",hsb);
add("East",vsb);
hsb.addAdjustmentListener(new AdjustmentListener(){
public void adjustmentValueChanged(AdjustmentEvent ae){
h=hsb.getValue();
v=vsb.getValue();
showStatus(h+""+","+v);
repaint();
}
});
vsb.addAdjustmentListener(new AdjustmentListener(){
public void adjustmentValueChanged(AdjustmentEvent ae){
h=hsb.getValue();
v=vsb.getValue();
showStatus(h+""+","+v);
repaint();
}
});
}
public void paint(Graphics g){
g.drawOval(50,50,h,v);
}
}
Bye...
 
Rajesh Radh
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sanjay.
But I was trying to find out a different thing for a specific purpose. I Have a JList that is attached to a JScrollPane. The JScrollPane uses an inner class called ScrollBar(subclass of JScrollBar) to handle its scrolling needs. The associated UI with JScrollBar, which is BasicScrollBarUI uses an inner class, called ArrowButtonListener. I wanted to find out when user clicks on the JScrollPane's, which arrow button user clicks, i.e. up /down arrow associated with a scrollbar (please do not confused with keyboard up and down arrow). I know I can subclass the BasicScrollBarUI class and add functionality to get it. I wanted to find out if there is any easy way to do it?
Please any one?
Thanks,
Rajesh
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!