Java Code Examples for java.awt.event.MouseWheelListener

The following examples show how to use java.awt.event.MouseWheelListener. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: FlatLaf   Source File: FlatScrollPaneUI.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected MouseWheelListener createMouseWheelListener() {
	return new BasicScrollPaneUI.MouseWheelHandler() {
		@Override
		public void mouseWheelMoved( MouseWheelEvent e ) {
			// Note: Getting UI value "ScrollPane.smoothScrolling" here to allow
			// applications to turn smooth scrolling on or off at any time
			// (e.g. in application options dialog).
			if( UIManager.getBoolean( "ScrollPane.smoothScrolling" ) &&
				scrollpane.isWheelScrollingEnabled() &&
				e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL &&
				e.getPreciseWheelRotation() != 0 &&
				e.getPreciseWheelRotation() != e.getWheelRotation() )
			{
				mouseWheelMovedSmooth( e );
			} else
				super.mouseWheelMoved( e );
		}
	};
}
 
Example 2
Source Project: Cafebabe   Source File: CFGraph.java    License: GNU General Public License v3.0 6 votes vote down vote up
public CFGComponent(mxGraph g) {
	super(g);
	this.getViewport().setBackground(Color.WHITE);
	this.setEnabled(false);
	this.setBorder(new EmptyBorder(0, 0, 0, 0));
	this.setZoomFactor(1.1);
	this.getGraphControl().addMouseWheelListener(new MouseWheelListener() {
		@Override
		public void mouseWheelMoved(MouseWheelEvent e) {
			if (e.isControlDown()) {
				if (e.getWheelRotation() < 0) {
					zoomIn();
				} else {
					zoomOut();
				}
				repaint();
				revalidate();
			} else if (scp != null) {
				// do we need this on linux too?
				scp.getVerticalScrollBar().setValue(scp.getVerticalScrollBar().getValue()
						+ e.getUnitsToScroll() * scp.getVerticalScrollBar().getUnitIncrement());
			}
		}
	});
}
 
Example 3
Source Project: netbeans   Source File: UIUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void issue163946Hack(final JScrollPane scrollPane) {
    MouseWheelListener listener = new MouseWheelListener() {
        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (scrollPane.getVerticalScrollBar().isShowing()) {
                if (e.getSource() != scrollPane) {
                    e.setSource(scrollPane);
                    scrollPane.dispatchEvent(e);
                }
            } else {
                scrollPane.getParent().dispatchEvent(e);
            }
        }
    };
    scrollPane.addMouseWheelListener(listener);
    scrollPane.getViewport().getView().addMouseWheelListener(listener);
}
 
Example 4
Source Project: scelight   Source File: MapCanvas.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new {@link MapCanvas}.
 * 
 * @param repProc replay processor
 * @param zoomComboBox combo box which tells how to zoom the map image
 */
public MapCanvas( final RepProcessor repProc, final XComboBox< Zoom > zoomComboBox ) {
	this.repProc = repProc;
	this.zoomComboBox = zoomComboBox;
	
	ricon = MapImageCache.getMapImage( repProc );
	
	GuiUtils.makeComponentDragScrollable( this );
	
	// Zoom in and out with CTRL+wheel scroll:
	addMouseWheelListener( new MouseWheelListener() {
		@Override
		public void mouseWheelMoved( final MouseWheelEvent event ) {
			if ( event.isControlDown() ) {
				final int newZoomIdx = zoomComboBox.getSelectedIndex() - event.getWheelRotation();
				zoomComboBox.setSelectedIndex( Math.max( 0, Math.min( zoomComboBox.getItemCount() - 1, newZoomIdx ) ) );
				// An event will be fired which will cause reconfigureZoom() to be called...
			}
		}
	} );
}
 
Example 5
Source Project: visualvm   Source File: JavaOverviewSummary.java    License: GNU General Public License v2.0 6 votes vote down vote up
HorizontalScroller(JComponent view) {
    super(view, VERTICAL_SCROLLBAR_NEVER, HORIZONTAL_SCROLLBAR_AS_NEEDED);

    setBorder(BorderFactory.createEmptyBorder());
    setViewportBorder(BorderFactory.createEmptyBorder());

    getViewport().setOpaque(false);
    setOpaque(false);
    
    super.addMouseWheelListener(new MouseWheelListener() {
        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (e.getModifiers() == MouseWheelEvent.SHIFT_MASK) {
                scroll(getHorizontalScrollBar(), e);
            } else {
                getParent().dispatchEvent(e);
            }
        }
        
    });
}
 
Example 6
Source Project: visualvm   Source File: ProfilerTabbedPane.java    License: GNU General Public License v2.0 6 votes vote down vote up
public ProfilerTabbedPane() {
    setFocusable(false);
    
    addMouseWheelListener(new MouseWheelListener() {
        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (!mouseWheelScrolls()) return;
            
            int units = e.getWheelRotation(); // always step by 1!
            int selected = getSelectedIndex();
            
            int newSelected = selected + units;
            if (newSelected < 0) newSelected = 0;
            else if (newSelected >= getTabCount()) newSelected = getTabCount() - 1;
            
            setSelectedIndex(newSelected);
        }
    });
}
 
Example 7
Source Project: visualvm   Source File: ProfilerTabbedPane.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void addTab(String title, Icon icon, final Component component, String tip, boolean closable) {
    int tabCount = getTabCount();

    if (component.getMouseWheelListeners().length == 0 && UIUtils.isAquaLookAndFeel()) {
        component.addMouseWheelListener(new MouseWheelListener() {
            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                // GH-122
            }
        });
    }
    super.addTab(title, icon, component, tip);
    
    Runnable closer = closable ? new Runnable() {
        public void run() {
            closeTab(component);
        }
    } : null;
    
    setTabComponentAt(tabCount, new TabCaption(title, icon, closer));
}
 
Example 8
Source Project: visualvm   Source File: ProfilerTabbedPane.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void insertTab(String title, Icon icon, final Component component, String tip, boolean closable, int index) {
    if (component.getMouseWheelListeners().length == 0 && UIUtils.isAquaLookAndFeel()) {
        component.addMouseWheelListener(new MouseWheelListener() {
            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                // GH-122
            }
        });
    }
    super.insertTab(title, icon, component, tip, index);
    
    Runnable closer = closable ? new Runnable() {
        public void run() {
            closeTab(component);
        }
    } : null;
    
    setTabComponentAt(index, new TabCaption(title, icon, closer));
}
 
Example 9
public CgSpinnerDouble(double start, double min, double max, double step) {
	super();
	this.min = min;
	this.max = max;
	this.step = step;

	model = new SpinnerNumberModel(start, // initial value
			min, // min
			max, // max
			step);

	this.setModel(model);

	addMouseWheelListener(new MouseWheelListener() {
		public void mouseWheelMoved(MouseWheelEvent mwe) {
			MouseWheelAction(mwe.getWheelRotation());
		}
	});

	// Center
	JSpinner.DefaultEditor spinnerEditor = (JSpinner.DefaultEditor) this.getEditor();
	spinnerEditor.getTextField().setHorizontalAlignment(JTextField.CENTER);
}
 
Example 10
Source Project: Course_Generator   Source File: CgSpinner.java    License: GNU General Public License v3.0 6 votes vote down vote up
public CgSpinner(int start, int min, int max, int step) {
	super();
	this.min = min;
	this.max = max;
	this.step = step;

	model = new SpinnerNumberModel(start, // initial value
			min, // min
			max, // max
			step); // step
	setModel(model);

	addMouseWheelListener(new MouseWheelListener() {
		public void mouseWheelMoved(MouseWheelEvent mwe) {
			MouseWheelAction(mwe.getWheelRotation());
		}
	});

	// Center
	JSpinner.DefaultEditor spinnerEditor = (JSpinner.DefaultEditor) this.getEditor();
	spinnerEditor.getTextField().setHorizontalAlignment(JTextField.CENTER);
}
 
Example 11
/**
 * Install this class as the default {@link MouseWheelListener} for {@link MouseWheelEvent MouseWheelEvents}.
 *
 * Original listeners will be moved to the realListeners. Can be undone via {@link MouseWheelController#uninstall()}.
 */
public void install() {
	if (realListeners != null) {
		return;
	}

	//  Keep track of original listeners so we can use them to redispatch an altered MouseWheelEvent
	realListeners = scrollPane.getMouseWheelListeners();

	for (MouseWheelListener mwl : realListeners) {
		scrollPane.removeMouseWheelListener(mwl);
	}

	//  Intercept events so they can be redispatched
	scrollPane.addMouseWheelListener(this);
}
 
Example 12
/**
 * Remove the class as the default {@link MouseWheelListener} and reinstall the original listeners.
 */
public void uninstall() {
	if (realListeners == null) {
		return;
	}

	//  Remove this class as the default listener
	scrollPane.removeMouseWheelListener(this);

	//  Install the default listeners
	for (MouseWheelListener mwl : realListeners) {
		scrollPane.addMouseWheelListener(mwl);
	}

	realListeners = null;
}
 
Example 13
Source Project: zap-extensions   Source File: JScrollPopupMenu.java    License: Apache License 2.0 6 votes vote down vote up
public JScrollPopupMenu(String label) {
    super(label);
    setLayout(new ScrollPopupMenuLayout());

    super.add(getScrollBar());
    addMouseWheelListener(new MouseWheelListener() {
        @Override public void mouseWheelMoved(MouseWheelEvent event) {
            JScrollBar scrollBar = getScrollBar();
            int amount = (event.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
                         ? event.getUnitsToScroll() * scrollBar.getUnitIncrement()
                         : (event.getWheelRotation() < 0 ? -1 : 1) * scrollBar.getBlockIncrement();

            scrollBar.setValue(scrollBar.getValue() + amount);
            event.consume();
        }
    });
}
 
Example 14
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
private static JComboBox<String> makeComboBox() {
  return new JComboBox<String>(makeModel()) {
    private transient MouseWheelListener handler;
    @Override public void updateUI() {
      removeMouseWheelListener(handler);
      super.updateUI();
      handler = e -> {
        JComboBox<?> source = (JComboBox<?>) e.getComponent();
        if (!source.hasFocus()) {
          return;
        }
        int ni = source.getSelectedIndex() + e.getWheelRotation();
        if (ni >= 0 && ni < source.getItemCount()) {
          source.setSelectedIndex(ni);
        }
      };
      addMouseWheelListener(handler);
    }
  };
}
 
Example 15
@Override
protected void installListeners() {
    super.installListeners();
    if (mouseListener instanceof MouseWheelListener) {
        graph.addMouseWheelListener((MouseWheelListener) mouseListener);
    }
}
 
Example 16
@Override
protected void uninstallListeners() {
    super.uninstallListeners();
    if (mouseListener instanceof MouseWheelListener) {
        graph.removeMouseWheelListener((MouseWheelListener) mouseListener);
    }
}
 
Example 17
Source Project: amodeus   Source File: JMapController.java    License: GNU General Public License v2.0 5 votes vote down vote up
public JMapController(JMapViewer map) {
    this.map = map;
    if (this instanceof MouseListener)
        map.addMouseListener((MouseListener) this);
    if (this instanceof MouseWheelListener)
        map.addMouseWheelListener((MouseWheelListener) this);
    if (this instanceof MouseMotionListener)
        map.addMouseMotionListener((MouseMotionListener) this);
}
 
Example 18
Source Project: netbeans   Source File: Preview.java    License: Apache License 2.0 5 votes vote down vote up
public void addMouseWheelListener(MouseWheelListener listener) {
            if (myMouseWheelListeners == null) {
                myMouseWheelListeners = new ArrayList<MouseWheelListener>();
            }
//out("Listener: " + listener.getClass().getName());
            myMouseWheelListeners.add(listener);
        }
 
Example 19
Source Project: netbeans   Source File: Preview.java    License: Apache License 2.0 5 votes vote down vote up
public void addWheelListener(MouseWheelListener wheelListener) {
    super.addMouseWheelListener(wheelListener);

    for (int i = 0; i < myMouseWheelListeners.size(); i++) {
        super.addMouseWheelListener(myMouseWheelListeners.get(i));
    }
}
 
Example 20
Source Project: netbeans   Source File: SmoothScrollPaneUI.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected MouseWheelListener createMouseWheelListener() {
    final MouseWheelListener delegate = super.createMouseWheelListener();
    return new MouseWheelListener() {
        @Override
        public void mouseWheelMoved(MouseWheelEvent evt) {
            handleMouseWheelEvent(evt, delegate);
        }
    };
}
 
Example 21
Source Project: netbeans   Source File: SmoothScrollPaneUI.java    License: Apache License 2.0 5 votes vote down vote up
private void handleMouseWheelEvent(MouseWheelEvent evt, MouseWheelListener delegate) {
    if (scrollpane.isWheelScrollingEnabled() &&
        evt.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
    {
        mouseWheelMoved(evt);
        evt.consume();
    } else {
        delegate.mouseWheelMoved(evt);
    }
}
 
Example 22
Source Project: netbeans   Source File: ChartPanel.java    License: Apache License 2.0 5 votes vote down vote up
private void setWheelScrollHandler(final JScrollBar scrollBar) {
    chart.addMouseWheelListener(new MouseWheelListener() {
        public void mouseWheelMoved(MouseWheelEvent e) {
            scroll(scrollBar, e);
        }
    });
}
 
Example 23
Source Project: netbeans   Source File: JExtendedSpinner.java    License: Apache License 2.0 5 votes vote down vote up
private void configureWheelListener() {
    addMouseWheelListener(new MouseWheelListener() {
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (e.getScrollType() != MouseWheelEvent.WHEEL_UNIT_SCROLL) return;
            Object newValue = (e.getWheelRotation() < 0 ?
                               JExtendedSpinner.this.getNextValue() :
                               JExtendedSpinner.this.getPreviousValue());
            if (newValue != null) JExtendedSpinner.this.setValue(newValue);
        }
    });
}
 
Example 24
Source Project: netbeans   Source File: DocumentViewOp.java    License: Apache License 2.0 5 votes vote down vote up
private void updateListeners() {
    ScrollPaneUI ui = scrollPane.getUI();
    if (ui != lastUI) {
        // Update "ui" property listener
        if (ui != null) {
            // Check mouse wheel listeners on scroll pane.
            // Pair first non-MWDelegator listener with 
            // Remove any other delegators than this one.
            MouseWheelListener[] mwls = scrollPane.getListeners(MouseWheelListener.class);
            if (LOG.isLoggable(Level.FINE)) {
                LOG.log(Level.FINE, "MouseWheelDelegator.updateListeners(): scrollPane change scrollPane={0}, MouseWheelListeners:{1}\n",
                        new Object[]{obj2String(scrollPane), Arrays.asList(mwls)});
            }
            delegateListener = null;
            for (MouseWheelListener listener : mwls) {
                if (listener instanceof MouseWheelDelegator) {
                    scrollPane.removeMouseWheelListener(listener);
                    if (delegateListener == null) {
                        delegateListener = ((MouseWheelDelegator) listener).delegateListener;
                        scrollPane.addMouseWheelListener(this);
                    }
                } else { // Regular listener 
                    // Current patch only assumes one MW listener attached by the UI impl.
                    if (delegateListener == null) {
                        delegateListener = listener;
                        scrollPane.removeMouseWheelListener(listener);
                        scrollPane.addMouseWheelListener(this);
                    }
                }
            }
        }
        lastUI = ui;
    }
}
 
Example 25
Source Project: netbeans   Source File: DocumentViewOp.java    License: Apache License 2.0 5 votes vote down vote up
void delegateToOriginalListener(MouseWheelEvent e, JScrollPane activeScrollPane) {
    // Only delegate if the updated scrollpane is still actively used by the DocumentViewOp
    // and when it still uses the overriden listener (this)
    if (activeScrollPane == scrollPane) {
        MouseWheelListener[] mwls = scrollPane.getListeners(MouseWheelListener.class);
        for (int i = 0; i < mwls.length; i++) {
            if (mwls[i] == this) {
                delegateListener.mouseWheelMoved(e);
                return;
            }
        }
    }
}
 
Example 26
Source Project: ramus   Source File: PrintPreviewComponent.java    License: GNU General Public License v3.0 5 votes vote down vote up
public PrintPreviewComponent(RamusPrintable printable, int columnCount,
                             GUIFramework framework) {
    this.printable = printable;
    this.columnCount = columnCount;
    this.framework = framework;
    MouseWheelListener l = new MouseWheelListener() {

        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
                if (e.getModifiers() == KeyEvent.CTRL_MASK) {
                    double r = e.getWheelRotation();
                    double zoom = getZoom() - 0.2 * r;
                    setCurrentZoom(zoom);
                } else {
                    Rectangle rect = getVisibleRect();
                    scrollRectToVisible(new Rectangle(rect.x, rect.y
                            + e.getWheelRotation() * 150, rect.width,
                            rect.height));
                }
            }
        }
    };
    this.addMouseWheelListener(l);
    layout = Options.getInteger("PREVIW_LAYOUT", PREV_LAYOUT_GRID);
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            setCurrentZoom(Options.getDouble("PREV_ZOOM", 1d));
        }
    });
    setCurrentZoom(Options.getDouble("PREV_ZOOM", 1d));
}
 
Example 27
Source Project: audiveris   Source File: Rubber.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Called when the mouse wheel is moved.
 * If CTRL key is down, modify current zoom ratio accordingly, otherwise
 * forward the wheel event to proper container (JScrollPane usually).
 *
 * @param e the mouse wheel event
 */
@Override
public void mouseWheelMoved (MouseWheelEvent e)
{
    // CTRL is down?
    if (e.isControlDown()) {
        double ratio = zoom.getRatio();

        if (e.getWheelRotation() > 0) {
            ratio /= factor;
        } else {
            ratio *= factor;
        }

        zoom.setRatio(ratio);
    } else {
        // Forward event to some container of the component?
        Container container = component.getParent();

        while (container != null) {
            if (container instanceof JComponent) {
                JComponent comp = (JComponent) container;
                MouseWheelListener[] listeners = comp.getMouseWheelListeners();

                if (listeners.length > 0) {
                    for (MouseWheelListener listener : listeners) {
                        listener.mouseWheelMoved(e);
                    }

                    return;
                }
            }

            container = container.getParent();
        }
    }
}
 
Example 28
Source Project: visualvm   Source File: ChartPanel.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void setWheelScrollHandler(final JScrollBar scrollBar) {
    chart.addMouseWheelListener(new MouseWheelListener() {
        public void mouseWheelMoved(MouseWheelEvent e) {
            scroll(scrollBar, e);
        }
    });
}
 
Example 29
Source Project: visualvm   Source File: JExtendedSpinner.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void configureWheelListener() {
    addMouseWheelListener(new MouseWheelListener() {
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (e.getScrollType() != MouseWheelEvent.WHEEL_UNIT_SCROLL) return;
            Object newValue = (e.getWheelRotation() < 0 ?
                               JExtendedSpinner.this.getNextValue() :
                               JExtendedSpinner.this.getPreviousValue());
            if (newValue != null) JExtendedSpinner.this.setValue(newValue);
        }
    });
}
 
Example 30
Source Project: visualvm   Source File: JExtendedSpinner.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void configureWheelListener() {
    addMouseWheelListener(new MouseWheelListener() {
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (e.getScrollType() != MouseWheelEvent.WHEEL_UNIT_SCROLL) return;
            Object newValue = (e.getWheelRotation() < 0 ?
                               JExtendedSpinner.this.getNextValue() :
                               JExtendedSpinner.this.getPreviousValue());
            if (newValue != null) JExtendedSpinner.this.setValue(newValue);
        }
    });
}