java.awt.event.MouseWheelListener Java Examples

The following examples show how to use java.awt.event.MouseWheelListener. 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 File: ProfilerTabbedPane.java    From visualvm with 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 #2
Source File: UIUtils.java    From netbeans with 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 #3
Source File: CgSpinnerDouble.java    From Course_Generator with GNU General Public License v3.0 6 votes vote down vote up
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 #4
Source File: CgSpinner.java    From Course_Generator with 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 #5
Source File: MouseWheelController.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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 #6
Source File: MouseWheelController.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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 #7
Source File: JScrollPopupMenu.java    From zap-extensions with 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 #8
Source File: MainPanel.java    From java-swing-tips with 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 #9
Source File: CFGraph.java    From Cafebabe with 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 #10
Source File: FlatScrollPaneUI.java    From FlatLaf with 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 #11
Source File: MapCanvas.java    From scelight with 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 #12
Source File: JavaOverviewSummary.java    From visualvm with 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 #13
Source File: ProfilerTabbedPane.java    From visualvm with 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 #14
Source File: ProfilerTabbedPane.java    From visualvm with 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 #15
Source File: JExtendedSpinner.java    From visualvm with 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 #16
Source File: ChartPanel.java    From visualvm with 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 #17
Source File: CharMap4Grid.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Instantiates a new char map4 grid.
 *
 * @param charMap4
 */
public CharMap4Grid(CharMap4 charMap4) {
    super(); // initialize our superclass first (JPanel)

    this.charMap4 = charMap4;
    // Set class instance variables to undefined values that we will recognise
    // if we are called before the layout and first "paint" is complete. */

    cellCount = charCount = glyphCount = 0; // no chars or glyphs to display
    clickIndex = NO_MOUSE; // cell index of clicked character
    clickStartX = clickStartY = NO_MOUSE; // no starting coordinates for click
    cornerIndex = 0; // cell index of top-left corner
    fontData = null; // information about current display font
    horizStep = 100; // horizontal offset from one cell to next
    hoverIndex = NO_MOUSE; // cell index of mouse over character
    lineAscent = 100; // number of pixels above baseline
    lineHeight = 100; // height of each display line in pixels
    maxWidth = 100; // maximum pixel width of all characters
    panelColumns = 10; // number of complete text columns displayed
    panelCount = -1; // saved value of <cellCount> used previously
    panelFont = null; // saved font for drawing text on this panel
    panelHeight = panelWidth = -1; // saved panel height and width in pixels
    panelRows = 10; // number of complete lines (rows) displayed
    vertiStep = 100; // vertical offset from one cell to next

    /* Install our mouse and scroll listeners. */

    this.addMouseListener((MouseListener) this);
    this.addMouseMotionListener((MouseMotionListener) this);
    this.addMouseWheelListener((MouseWheelListener) this);
    // this.setFocusable(false); // we don't handle keyboard input, owner does

}
 
Example #18
Source File: JMapController.java    From Course_Generator with GNU General Public License v3.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 #19
Source File: JExtendedSpinner.java    From visualvm with 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 #20
Source File: MouseWheelController.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Redispatch a {@link MouseWheelEvent} to the real {@link MouseWheelListener MouseWheelListeners}
 */
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
	//  Create an altered event to redispatch
	if (scrollAmount != 0) {
		e = createScrollAmountEvent(e);
	}

	//  Redispatch the event to original MouseWheelListener
	for (MouseWheelListener mwl : realListeners) {
		mwl.mouseWheelMoved(e);
	}
}
 
Example #21
Source File: SwingComponent.java    From jexer with MIT License 5 votes vote down vote up
/**
 * Adds the specified mouse wheel listener to receive mouse wheel events
 * from this component. Containers also receive mouse wheel events from
 * sub-components.
 *
 * @param l the mouse wheel listener
 */
public void addMouseWheelListener(MouseWheelListener l) {
    if (frame != null) {
        frame.addMouseWheelListener(l);
    } else {
        component.addMouseWheelListener(l);
    }
}
 
Example #22
Source File: ScrollablePopupMenu.java    From jaamsim with Apache License 2.0 5 votes vote down vote up
public ScrollablePopupMenu(String label) {
	super(label);

	// First component is the scroll bar
	scrollBar = new JScrollBar();
	scrollBar.setVisible(false);
	scrollBar.addAdjustmentListener(new AdjustmentListener() {
		@Override
		public void adjustmentValueChanged(AdjustmentEvent e) {
			doLayout();
			repaint();
		}
	});
	super.add(scrollBar);

	// Use a customised LayoutManager to position the scroll bar correctly
	setLayout(new ScrollableMenuLayout());

	// Respond to the mouse wheel
	addMouseWheelListener(new MouseWheelListener() {
		@Override
		public void mouseWheelMoved(MouseWheelEvent event) {
			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 #23
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static JTabbedPane makeWheelTabbedPane() {
  return new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT) {
    private transient MouseWheelListener handler;

    @Override public void updateUI() {
      removeMouseWheelListener(handler);
      super.updateUI();
      handler = e -> {
        JTabbedPane source = (JTabbedPane) e.getComponent();
        if (!getTabAreaBounds(source).contains(e.getPoint())) {
          return;
        }
        double dir = (e.isControlDown() ? -1 : 1) * e.getPreciseWheelRotation();
        String key = dir > 0 ? "navigateNext" : "navigatePrevious";
        ActionEvent ae = new ActionEvent(source, ActionEvent.ACTION_PERFORMED, "", e.getWhen(), e.getModifiersEx());
        source.getActionMap().get(key).actionPerformed(ae);
        // int idx = source.getSelectedIndex() + (int) dir;
        // if (idx < 0) {
        //   idx = source.getTabCount() - 1;
        // } else if (idx >= source.getTabCount()) {
        //   idx = 0;
        // }
        // source.setSelectedIndex(idx);
      };
      addMouseWheelListener(handler);
    }
  };
}
 
Example #24
Source File: Rubber.java    From libreveris with GNU Lesser 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 #25
Source File: TimeSeriesMatrixTopComponent.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private void addMouseWheelListener() {
    if (currentView != null) {
        final LayerCanvas layerCanvas = currentView.getLayerCanvas();
        final List<MouseWheelListener> listeners = Arrays.asList(layerCanvas.getMouseWheelListeners());
        if (!listeners.contains(mouseWheelListener)) {
            layerCanvas.addMouseWheelListener(mouseWheelListener);
        }
    }
}
 
Example #26
Source File: ReportLayoutPanel.java    From nextreports-designer with Apache License 2.0 5 votes vote down vote up
private void initZoomListeners() {
	addZoomListener(reportGridPanel);
	// zoom with CTRL & mouse wheel
	reportGridPanel.addMouseWheelListener(new MouseWheelListener() {

		public void mouseWheelMoved(MouseWheelEvent e) {
			if ((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) {
				int clicks = e.getWheelRotation();
				int zoomValue;
				if (clicks < 0) {
					// zoom in
					zoomValue = ((Integer) spinnerModel.getValue()).intValue() + zoomDelta;
				} else {
					// zoom out
					zoomValue = ((Integer) spinnerModel.getValue()).intValue() - zoomDelta;
				}
				if (zoomValue < minZoomValue) {
					zoomValue = minZoomValue;
				} else if (zoomValue > maxZoomValue) {
					zoomValue = maxZoomValue;
				}
				spinnerModel.setValue(zoomValue);
				Integer percentage = (Integer) spinnerModel.getValue();
				ZoomEvent zoomEvent = new ZoomEvent(percentage / 100f);
				fireZoomEvent(zoomEvent);
			}
		}
	});
}
 
Example #27
Source File: ComboBoxMouseWheelScrollBehavior.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Uninstalls all behaviors from the specified combobox.
 *
 * @param comboBox combobox to modify
 */
public static void uninstall ( @NotNull final JComboBox comboBox )
{
    for ( final MouseWheelListener listener : comboBox.getMouseWheelListeners () )
    {
        if ( listener instanceof ComboBoxMouseWheelScrollBehavior )
        {
            comboBox.removeMouseWheelListener ( listener );
        }
    }
}
 
Example #28
Source File: ComboBoxMouseWheelScrollBehavior.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns whether the specified combobox has any behaviors installed or not.
 *
 * @param comboBox combobox to process
 * @return true if the specified combobox has any behaviors installed, false otherwise
 */
public static boolean isInstalled ( @NotNull final JComboBox comboBox )
{
    for ( final MouseWheelListener listener : comboBox.getMouseWheelListeners () )
    {
        if ( listener instanceof ComboBoxMouseWheelScrollBehavior )
        {
            return true;
        }
    }
    return false;
}
 
Example #29
Source File: ChartPanel.java    From netbeans with 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 #30
Source File: ERDesignerGraphUI.java    From MogwaiERDesignerNG with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void uninstallListeners() {
    super.uninstallListeners();
    if (mouseListener instanceof MouseWheelListener) {
        graph.removeMouseWheelListener((MouseWheelListener) mouseListener);
    }
}