Re: Custom JScrollPane - Double JScrollBars
On Apr 29, 5:07 pm, "David A. Redick" <tinyweldingto...@gmail.com>
wrote:
I played around some more this morning and I think I figured it out.
Just add the SpringyScrollBar class and replace the DoubleScrollPane
class.
The rest is the same.
class SpringyScrollBar extends JScrollBar
{
public SpringyScrollBar(int iOrientation)
{
super(iOrientation);
setMaximum(99);
setMinimum(0);
moveToMid();
}
public int getMid()
{
int iMid = getMaximum() - getMinimum() + 1;
iMid /= 2;
int iSize = getVisibleAmount()/2;
iMid -= iSize;
return iMid;
}
public void moveToMid()
{
setValue(getMid());
}
public int getSpeed()
{
int i = getValue();
i -= getMid();
if(i < 0)
i *= -1;
return i;
}
public int getDir()
{
int i = getValue();
i -= getMid();
if(i < 0)
return -1;
return 1;
}
}
// 2 horiz. bars and 2 vert. bars.
class DoubleScrollPane extends JScrollPane implements
AdjustmentListener
{
protected SpringyScrollBar m_pHSB2;
protected SpringyScrollBar m_pVSB2;
DoubleScrollPane(Component pView)
{
super(pView);
setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
m_pHSB2 = new SpringyScrollBar(JScrollBar.HORIZONTAL);
m_pVSB2 = new SpringyScrollBar(JScrollBar.VERTICAL);
m_pHSB2.addAdjustmentListener(this);
m_pVSB2.addAdjustmentListener(this);
m_pHSB2.setVisible(true);
m_pVSB2.setVisible(true);
add(m_pHSB2);
add(m_pVSB2);
setLayout(new DoubleScrollPaneLayout(m_pHSB2, m_pVSB2));
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
SpringyScrollBar p = (SpringyScrollBar) e.getAdjustable();
boolean bIsHoriz = p.equals(m_pHSB2);
int iSpeed = p.getSpeed();
int iDir = p.getDir();
// Adjust position.
JScrollBar pBar;
if(bIsHoriz)
pBar = horizontalScrollBar;
else
pBar = verticalScrollBar;
int iInc = pBar.getUnitIncrement(iDir);
iInc *= iSpeed;
int iValue = pBar.getValue();
iValue += iDir*iInc;
pBar.setValue(iValue);
if(!e.getValueIsAdjusting())
{
// move back to center
p.moveToMid();
}
}
}
Amazing! ;) Exactly how I wanted it! I can't thank you enough. I'll
tweak some things and report back. Oh. In case you have any website/
blog, I'd like to hear about it. I want to post the source code in my
blog and have some kind of reference to you.. ;)
By the way, there is a little problem. I'm running Linux, and when the
knob is being dragged, it moves the view perfectly. But if let's say,
I drag the knob from the middle to 1/3 position and just hold the knob
there, the view doesn't move. As long as the knob is being dragged it
does. I hope you understand what I'm saying.
I tried adding MouseMotionListener and MouseListener but again, when
the knob is still, no event is fired, no matter in which position it
is.
Other than that, excellent! ;)