Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Automaticaly scroll when mouse (drag) goes out of window ?

132 views
Skip to first unread message

Alain Birtz

unread,
Jan 22, 2002, 8:23:12 AM1/22/02
to
Is it possible to force scrollPane to move when the mouse cursor
(is down for dragging) goes outside of the window ?

Where can I found a Java source code about it ?

--
Alain Birtz
Cegep St-Hyacinthe College
Internet a...@videotron.ca, abi...@cegepsth.qc.ca

Dave Ortman

unread,
Jan 22, 2002, 11:22:07 AM1/22/02
to
"Alain Birtz" <a...@videotron.ca> wrote in message
news:abz-220102...@ppp152.33-113-216.mtl.mt.videotron.ca...

> Is it possible to force scrollPane to move when the mouse cursor
> (is down for dragging) goes outside of the window ?
>
> Where can I found a Java source code about it ?

I have this code, which may help. Drag a file into the window; when you
move the mouse to the bottom of the window, the ScrollPane will
automatically scroll. It doesn't work if the mouse exits the ScrollPane;
but by simply dragging the mouse to the bottom (or right side) of the
window, you can tigger auto scroll.

-Dave Ortman

package ortman.misc;

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TestTable extends JTable implements Autoscroll {

protected Point viewLocation;

public static void main(String[] args) {
JFrame f = new JFrame("TestTable");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(new TestTable()));
f.setSize(500, 350);
f.setVisible(true);
}

public TestTable() {
super();
setModel(new DefaultTableModel(50, 35));
setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
setRowHeight(25);
new DropTarget(this, DnDConstants.ACTION_MOVE,
new DropTableListener());
}

protected Rectangle getTableRect() {
JViewport jvp = (JViewport)(SwingUtilities.getAncestorOfClass(
JViewport.class, this));
return (jvp == null ? null : jvp.getViewRect());
}

public Insets getAutoscrollInsets() {
Insets insets = new Insets(0, 0, 0, 0);
Rectangle rect = getTableRect();
if (rect != null) {
insets.top = rect.y + 20;
insets.left = rect.x + 20;
insets.bottom = getHeight() - (rect.y + rect.height) + 20;
insets.right = getWidth() - (rect.x + rect.width) + 20;
}
return insets;
}

protected void scheduleViewportUpdate() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JScrollBar scrollBar;
Point p;
synchronized (this) {
p = viewLocation;
}
JScrollPane jsp = (JScrollPane)(
SwingUtilities.getAncestorOfClass(
JScrollPane.class, TestTable.this));
scrollBar = jsp.getHorizontalScrollBar();
scrollBar.setValue(p.x);
scrollBar = jsp.getVerticalScrollBar();
scrollBar.setValue(p.y);
}
});
}

public synchronized void autoscroll(Point p) {
int offset;
Insets insets = getAutoscrollInsets();
Rectangle rect = getTableRect();
JViewport jvp = (JViewport)(SwingUtilities.getAncestorOfClass(
JViewport.class, this));
if (jvp != null) {
Point oldLocation = jvp.getViewPosition();
if (p.y < insets.top) {
offset = getScrollableUnitIncrement(rect,
SwingConstants.VERTICAL, -1);
viewLocation = new Point(oldLocation.x,
oldLocation.y - offset);
}
if (p.x < insets.left) {
offset = getScrollableUnitIncrement(rect,
SwingConstants.HORIZONTAL, -1);
viewLocation = new Point(oldLocation.x - offset,
oldLocation.y);
}
if (p.y > getHeight() - insets.bottom) {
offset = getScrollableUnitIncrement(rect,
SwingConstants.VERTICAL, 1);
viewLocation = new Point(oldLocation.x,
oldLocation.y + offset);
}
if (p.x > getWidth() - insets.right) {
offset = getScrollableUnitIncrement(rect,
SwingConstants.HORIZONTAL, 1);
viewLocation = new Point(oldLocation.x + offset,
oldLocation.y);
}

if (!(oldLocation.equals(viewLocation))) {
scheduleViewportUpdate();
}
}
}


class DropTableListener implements DropTargetListener {

public void dragEnter(DropTargetDragEvent event) {
event.acceptDrag(DnDConstants.ACTION_MOVE);
}

public void dragOver(DropTargetDragEvent event) {
event.acceptDrag(DnDConstants.ACTION_MOVE);
}

public void dragExit(DropTargetEvent event) {};
public void dropActionChanged(DropTargetDragEvent event) {};

public void drop(DropTargetDropEvent event) {
event.dropComplete(true);
}
}
}


0 new messages