|
Yes, I encountered the same problem... I got part (or all)
of this code somewhere on the internet. Use this for your
JTree...
import java.awt.*; import java.awt.dnd.*; import
java.awt.datatransfer.*; import java.awt.event.*; import
java.io.*; import java.util.*; import javax.swing.*; import
javax.swing.tree.*;
public class AutoScrollingJTree extends JTree implements
Autoscroll { private int margin = 12;
public AutoScrollingJTree() {
super(); }
public AutoScrollingJTree(DefaultMutableTreeNode
dmtn) { super(dmtn); } // Ok, we’ve
been told to scroll because the mouse cursor is in our // scroll
zone. public void autoscroll(Point p) {
int realrow = getRowForLocation(p.x,
p.y); Rectangle outer = getBounds();
realrow = (p.y + outer.y <= margin ?
realrow < 1 ? 0 : realrow - 1
: realrow < getRowCount() - 1 ? realrow + 1 :
realrow); scrollRowToVisible(realrow);
}
// Calculate the insets for the *JTREE*, not the
viewport // the tree is in. This makes it a bit messy.
public Insets getAutoscrollInsets() { Rectangle
outer = getBounds(); Rectangle inner =
getParent().getBounds(); return new
Insets( inner.y - outer.y + margin, inner.x - outer.x +
margin, outer.height - inner.height - inner.y + outer.y +
margin, outer.width - inner.width - inner.x + outer.x +
margin); }
}
Does anyone know how to make a Swing
JScrollPane autoscroll during a drag? For example, if I have a JTree in a
JScrollPane, and the tree has nodes not visible, I should be able to “drag and
drop” onto one of the non-visible nodes by autoscrolling during the drag and
drop operation. Any ideas? I have tried
JScrollPane.SetAutoScrolls(true), but that does not seem to work.
Thanks,
David
L. Connerth
Director, Digital
Collections Development
Gannett Media
Technologies International
513.587.2941 Phone
513.241.7219 Fax
dconnerth@xxxxxxxxxxxxxxxx
|