Skip to content

Instantly share code, notes, and snippets.

@aterai
Last active December 20, 2015 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aterai/6133563 to your computer and use it in GitHub Desktop.
Save aterai/6133563 to your computer and use it in GitHub Desktop.
@see "Mouse Dragging ViewPort Scroll - Java Swing Tips" comment: http://java-swing-tips.blogspot.jp/2009/03/mouse-dragging-viewport-scroll.html
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import javax.swing.*;
import javax.swing.plaf.LayerUI;
class DragScrollLayerTest {
public JComponent makeUI() {
JTabbedPane tab1 = new JTabbedPane();
tab1.addTab("aaa", new JLabel("11111111111"));
tab1.addTab("bbb", new JLabel("2222222222"));
JTabbedPane tab2 = new JTabbedPane();
tab2.addTab("ccccc", new JLabel("3333"));
tab2.addTab("ddddd", new JLabel("444444444444"));
Box box = Box.createVerticalBox();
box.add(new JLabel("aaaaaaaaaaaaaaaaaaaaaa"));
box.add(Box.createVerticalStrut(5));
box.add(tab1);
box.add(Box.createVerticalStrut(5));
box.add(new JCheckBox("bbbbbbbbbbbb"));
box.add(Box.createVerticalStrut(5));
box.add(tab2);
box.add(Box.createVerticalStrut(5));
box.add(new JButton("ccccc"));
box.add(Box.createVerticalStrut(5));
box.add(new JScrollPane(new JTree()));
box.add(Box.createVerticalStrut(5));
box.add(new JLabel("ddddddddddddddd"));
box.add(Box.createVerticalGlue());
box.setBorder(BorderFactory.createTitledBorder("Title"));
JScrollPane scroll = new JScrollPane(box);
scroll.getVerticalScrollBar().setUnitIncrement(25);
return new JLayer<JScrollPane>(scroll, new DragScrollLayerUI());
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new DragScrollLayerTest().makeUI());
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class DragScrollLayerUI extends LayerUI<JScrollPane> {
private final Point pp = new Point();
@Override public void installUI(JComponent c) {
super.installUI(c);
JLayer jlayer = (JLayer)c;
jlayer.setLayerEventMask(
AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK |
AWTEvent.MOUSE_WHEEL_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);
}
@Override public void uninstallUI(JComponent c) {
JLayer jlayer = (JLayer)c;
jlayer.setLayerEventMask(0);
super.uninstallUI(c);
}
@Override protected void processMouseEvent(MouseEvent e, JLayer<? extends JScrollPane> l) {
if(e.getID()==MouseEvent.MOUSE_PRESSED) {
JViewport vport = l.getView().getViewport();
Point cp = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), vport);
pp.setLocation(cp);
}
}
@Override protected void processMouseMotionEvent(MouseEvent e, JLayer<? extends JScrollPane> l) {
if(e.getID()==MouseEvent.MOUSE_DRAGGED) {
JViewport vport = l.getView().getViewport();
JComponent cmp = (JComponent)vport.getView();
Point cp = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), vport);
Point vp = vport.getViewPosition();
vp.translate(pp.x-cp.x, pp.y-cp.y);
cmp.scrollRectToVisible(new Rectangle(vp, vport.getSize()));
pp.setLocation(cp);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment