I want to override the mouse wheel listener in Swing but only if they have the Control button pressed. The listener will be attached to a JPanel so that when they scroll the wheel it will scroll the JScrollPane and when they have the control button pressed and scroll the wheel it will zoom in. The default scroll of JScrollPane works (obviously) before I override it with my own listener. Here is my code:
mainPanel.addMouseWheelListener(new MouseWheelListener(){
#Override
public void mouseWheelMoved(MouseWheelEvent e) {
if ((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) {
int notches = e.getWheelRotation();
if (notches < 0) {
redrawOnZoom(true);
} else {
redrawOnZoom(false);
}
}
}
});
Is there a way of saying something like "If mouse is scrolled on its own then do default JScrollPane scrolling behaviour but If Ctrl is pressed then zoom"?
you can dispatch the event to its parent if you don't want to handle it:
final MouseWheelListener wheel = new MouseWheelListener() {
#Override
public void mouseWheelMoved(MouseWheelEvent e) {
// handle some events here and dispatch others
if (shouldHandleHere(e)) {
LOG.info("do-my-own-stuff");
} else {
LOG.info("dispatch-to-parent");
e.getComponent().getParent().dispatchEvent(e);
}
}
public boolean shouldHandleHere(MouseWheelEvent e) {
return (e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0;
}
};
Hint:
Override the mouseWheelListener.
two functions : scroll() and other zoom.
Check for CTRL keyPress inside listener.
If pressed, call zoom() elsescroll()
refer tohow to write swing listener for guidence.
Related
I want to disable scrolling with the mousewheel in my JScrollPane while ctrl is pressed.
When you press ctrl and move the wheel you will zoom in/out AND also scroll the panel, which is not what I wanted.
Here's the working code:
scroller = new JScrollPane(view);
scroller.removeMouseWheelListener(scroller
.getMouseWheelListeners()[0]);
scroller.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(final MouseWheelEvent e) {
if (e.isControlDown()) {
if (e.getWheelRotation() < 0) {
// Zoom +
} else {
// Zoom -
}
} else if (e.isShiftDown()) {
// Horizontal scrolling
Adjustable adj = getScroller().getHorizontalScrollBar();
int scroll = e.getUnitsToScroll() * adj.getBlockIncrement();
adj.setValue(adj.getValue() + scroll);
} else {
// Vertical scrolling
Adjustable adj = getScroller().getVerticalScrollBar();
int scroll = e.getUnitsToScroll() * adj.getBlockIncrement();
adj.setValue(adj.getValue() + scroll);
}
}
});
Edited my question and resolved it myself.
If you have any tweaks go ahead and tell me!
Take a look at Mouse Wheel Controller. You won't be able to use the exact code but you should be able to use the concept of the class.
The code replaces the default MouseWheelListener with a custom listener. Then it recreates the event with one different parameter in redispatches the event to the default listeners.
In your case you won't need to create a new event you will just need to prevent any event with a Control modifier from being redispatched to the default listeners and instead you invoke the code you posted in your question.
In order to temporarily disable scrolling you could manipulate the scrollbar's unit increment value and, respectively, restore it again.
Just add a key listener to the view port panel and react to Ctrl key pressed:
editorPane.addKeyListener(new KeyAdapter(){
#Override
public void keyPressed(KeyEvent e) {
if ((e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0)
getVerticalScrollBar().setUnitIncrement(0);
else
getVerticalScrollBar().setUnitIncrement(15);
}
#Override
public void keyReleased(KeyEvent e) {
if ((e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0)
getVerticalScrollBar().setUnitIncrement(0);
else
getVerticalScrollBar().setUnitIncrement(15);
}
});
I want to show a small popup menu when you right-click a tab, now this is working fine but when you right click it also selects that tab which is unwanted.
So my idea was to make a new class, extend JTabbedPane and recode those mouse events. Problem is that I have no idea where to start, I was browsing its source but I can't find what part is handeling the mouseEvents.
Tabs.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent me) {
if(me.getButton()==3){
int tabNr = ((TabbedPaneUI)Tabs.getUI()).tabForCoordinate(Tabs, me.getX(), me.getY());
Component clickedTab = EventsConfig.window.MainTabs.getComponentAt(tabNr);
newMenu(clickedTab, me.getX(), me.getY());
}
}
});
Beware: dirty hack ahead! The only reason I recommend it, is that I consider the behaviour (select on right press) a bug in the BasicTabbedPaneUI's Handler.
The basic idea is to grab the listener installed by the ui, remove it, wrap into a custom listener which delegates everything except a right pressed to the original and add that to the pane:
private void installMouseListenerWrapper(JComponent tabbedPane) {
MouseListener handler = findUIMouseListener(tabbedPane);
tabbedPane.removeMouseListener(handler);
tabbedPane.addMouseListener(new MouseListenerWrapper(handler));
}
private MouseListener findUIMouseListener(JComponent tabbedPane) {
MouseListener[] listeners = tabbedPane.getMouseListeners();
for (MouseListener l : listeners) {
if (l.getClass().getName().contains("$Handler")) {
return l;
}
}
return null;
}
public static class MouseListenerWrapper implements MouseListener {
private MouseListener delegate;
public MouseListenerWrapper(MouseListener delegate) {
this.delegate = delegate;
}
#Override
public void mouseClicked(MouseEvent e) {
delegate.mouseClicked(e);
}
#Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) return;
delegate.mousePressed(e);
}
#Override
public void mouseReleased(MouseEvent e) {
delegate.mouseReleased(e);
}
#Override
public void mouseEntered(MouseEvent e) {
delegate.mouseEntered(e);
}
#Override
public void mouseExited(MouseEvent e) {
delegate.mouseExited(e);
}
}
then you have to add JPopupMenu (or JToolTip on MouseHoverOver ) to the JTabbedPane
A possible workaround is to set your custom tab component for each tab - see JTabbedPane#setTabComponentAt(...). Add a mouse handler to your custom tab component and redispatch left click events to the tabbedPane as described at http://www.jyloo.com/news/?pubId=1315817317000.
The custom tab component can be a simple JLabel (used for the tab title) or a container for multiple components. Depending on your requirements you can e.g. add an arrow button which will open a popup menu by left clicking the related button.
This article will helpful for removing unwanted tab selection when you click right mouse button.
Stop right click Event on JTabbedPane
I liked to add more about removing Mouse Listeners.
Try to override the method rather than removing it. It's better for future code updates.
The problem is BasicTabbedPaneUI's have inner class called Handler. That handler class override Mouse Listener.
To stop right click tab selection and show pop up menu; we need to override this method in BasicTabbedPaneUI,
protected MouseListener createMouseListener() {
return getHandler();
}
To get better look and feel we should override SynthTabbedPaneUI class.
SynthTabbedPaneUI is extends BasicTabbedPaneUI.
So our inner class is like this,
private class SynthTabbedPaneUIWrapper extends SynthTabbedPaneUI
{
private MouseAdapter menuAdapter;
private MouseAdapter getMenuAdapter()
{
if (menuAdapter == null)
{
menuAdapter =
new MouseAdapter()
{
#Override
public void mouseReleased(final MouseEvent e)
{
//implement to stop right click tab selection
//implement to show pop up menu
}
};
}
}
#Override
protected MouseListener createMouseListener()
{
return getMenuAdapter();
}
}
After that we can set our custom UI object into TabbedPane.
tabbedPane.setUI(new SynthTabbedPaneUIWrapper());
Basically there is a JPanel on which I want to know when the mouse enters the area of the JPanel and exits the area of the JPanel. So I added a mouse listener, but if there are components on the JPanel and the mouse goes over one of them it is detected as an exit on the JPanel, even though the component is on the JPanel. I was wondering whether anyone knows any way to solve this problem without doing something like adding listeners onto all components on the JPanel?
There is a very easy solution for this problem that can work :
public class MyJPanel implements MouseListener {
public void mouseExited(MouseEvent e) {
java.awt.Point p = new java.awt.Point(e.getLocationOnScreen());
SwingUtilities.convertPointFromScreen(p, e.getComponent());
if(e.getComponent().contains(p)) {return;}
...//the rest of your code
}
...
}
This way you just ignore the mouseExited event when it occurs on a child element.
Here is one way to do it for a component that may contain other components:
Add a global AWT event listener to get all mouse events. For example:
Toolkit.getDefaultToolkit().addAWTEventListener(
new TargetedMouseHandler( panel ), AWTEvent.MOUSE_EVENT_MASK );
Implement the TargetedMouseHandler to ignore events that aren't sourced by the panel or by one of the panel's children (you can use SwingUtilities.isDescendingFrom to test for this).
Keep track of whether or not the mouse is already within the bounds of your panel. When you get a MouseEvent.MOUSE_ENTERED event in your panel or one of its children, set a flag to true.
When you get a MouseEvent.MOUSE_EXITED event, only reset the flag if the point in the MouseEvent is outside the bounds of your target panel. SwingUtilities.convertPoint and Component.getBounds().contains() will come in handy here.
This is sample code implementing Ash's solution. For me, the JFrame did not detect all exit events properly, but an inner JPanel did, so I passed in two components - one for testing descendants and one for testing the boundary.
Toolkit.getDefaultToolkit().addAWTEventListener(
new TargetedMouseHandler(this, this.jPanel),
AWTEvent.MOUSE_EVENT_MASK);
}
public class TargetedMouseHandler implements AWTEventListener
{
private Component parent;
private Component innerBound;
private boolean hasExited = true;
public TargetedMouseHandler(Component p, Component p2)
{
parent = p;
innerBound = p2;
}
#Override
public void eventDispatched(AWTEvent e)
{
if (e instanceof MouseEvent)
{
if (SwingUtilities.isDescendingFrom(
(Component) e.getSource(), parent))
{
MouseEvent m = (MouseEvent) e;
if (m.getID() == MouseEvent.MOUSE_ENTERED)
{
if (hasExited)
{
System.out.println("Entered");
hasExited = false;
}
} else if (m.getID() == MouseEvent.MOUSE_EXITED)
{
Point p = SwingUtilities.convertPoint(
(Component) e.getSource(),
m.getPoint(),
innerBound);
if (!innerBound.getBounds().contains(p))
{
System.out.println("Exited");
hasExited = true;
}
}
}
}
}
}
A simpeler solution with java 1.8+
public class MyJPanel implements MouseListener {
public void mouseExited(MouseEvent e) {
if(!this.contains(e.getPoint())) {
... //the rest of your code
}
}
...
}
If you want to get all events sent to a top-level window you can add a listener to the glass pane of the JFrame. See getGlassPane.
I am trying to write a Minesweeper clone in Java for fun. I have a grid of JButtons whose labels I will change to represent the danger count, flags, etc.
My problem is, I don't know how to get a right click on a JButton to depress the button. I've done the following:
button.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
boolean mine = field.isMine(x, y);
if (e.isPopupTrigger()) {
button.setText("F");
}
else {
if (mine) {
button.setText("X");
}
}
}
});
This doesn't seem to be working at all; the "F" is never shown, only the "X" part. But more importantly, this does nothing for depressing the button.
EDIT: Macs have popup trigger happen on mousePress, not mouseClick.
EDIT: Here's the solution I worked out based off of accepted answer:
button.addMouseListener(new MouseAdapter(){
boolean pressed;
#Override
public void mousePressed(MouseEvent e) {
button.getModel().setArmed(true);
button.getModel().setPressed(true);
pressed = true;
}
#Override
public void mouseReleased(MouseEvent e) {
//if(isRightButtonPressed) {underlyingButton.getModel().setPressed(true));
button.getModel().setArmed(false);
button.getModel().setPressed(false);
if (pressed) {
if (SwingUtilities.isRightMouseButton(e)) {
button.setText("F");
}
else {
button.setText("X");
}
}
pressed = false;
}
#Override
public void mouseExited(MouseEvent e) {
pressed = false;
}
#Override
public void mouseEntered(MouseEvent e) {
pressed = true;
}
});
add(button);
Minesweeper clone http://grab.by/1y9z
Button can't be pressed by right click. Add such a lines to you mouse listener
mousePressed:
if(isRightButtonPressed) {underlyingButton.getModel().setPressed(true));
mouseReleased:
if(needReset) {underlyingButton.getModel().setPressed(false));
or do there whatever want.
I wouldn't use isPopupTrigger but directly check for the right button:
button.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
boolean mine = field.isMine(x, y);
if (e.getButton() == MouseEvent.BUTTON2) {
button.setText("F");
}
...
Just a small addition: the simplest way to check for the right button is SwingUtilities.isRightMouseButton
As you have mentioned that checking for "mousePressed" solved your issue. And the Javadoc of isPopupTrigger would explain the need for this:
public boolean isPopupTrigger()
...
Note: Popup menus are triggered differently on different systems. Therefore, isPopupTrigger should be checked in both mousePressed and mouseReleased for proper cross-platform functionality.
Also see the section on The Mouse Listener API in the Java Swing tutorial.
MouseEvent has some properties
static int BUTTON1
static int BUTTON2
static int BUTTON3
among others. Check those when your event fires.
EDIT
public int getButton()
Returns which, if any, of the mouse buttons has changed state.
The button being visibly depressed on right click isn't part of the "normal" behavior of buttons. You may be able to fake it using JToggleButtons, or simply changing the button's background color and maybe border while the right mouse button is being held down.
If you are certain that the event is properly being triggered (debug FTW!) and that the button.setText("F") is happening, then perhaps you simply need to repaint.
Repaint the button.
http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#repaint(java.awt.Rectangle)
This works for me fine on Mac:
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest extends JFrame {
JButton button;
public ButtonTest() {
button = new JButton("W");
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == 3) { // if right click
button.setText("F");
button.getModel().setPressed(false);
// button.setEnabled(true);
} else {
button.setText("X");
button.getModel().setPressed(true);
// button.setEnabled(false);
}
}
});
this.add(button);
this.setVisible(true);
}
public static void main(String[] args) {
new ButtonTest();
}
}
You might as well check for e.getButton() == 2 but I don't know when this one is triggered on Macs.
Using standalone SWT Scrollbars is something of a hack (using this workaround), but it can be done. Here's a snippet:
ScrolledComposite scrolledComposite = new ScrolledComposite(
parent, SWT.V_SCROLL);
ScrollBar scrollbar = scrolledComposite.getVerticalBar();
Shell tip = new Shell(UserInterface.getShell(), SWT.ON_TOP
| SWT.NO_FOCUS | SWT.TOOL);
// ..stylize and fill the tooltip..
Now what I'm trying to do is monitor when the user is interacting with the scrollbar. In particular, I want to know when the user is dragging the scrollbar—and when it has been released—in order to display an Office 2007-style tooltip revealing which page the position of the scrollbar corresponds with.
Presently, I have the following code which displays the tooltip:
scrollbar.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent event) {}
public void widgetSelected(SelectionEvent event) {
tip.setVisible(true);
}
}
It would seem logical then to have the tooltip disappear when the mouse button is released:
scrollbar.addListener(SWT.MouseUp, new Listener() {
public void handleEvent(Event event) {
tip.setVisible(false);
}
});
However, neither scrollbar nor scrolledComposite seem to respond to the SWT.MouseUp event when the user interacts with the scrollbar.
I presently have a workaround that hides the tip after a timeout, but I'm not satisfied with this. Any insights would be most appreciated!
Scrollbar's javadoc said this:
When widgetSelected is called, the
event object detail field contains one
of the following values: SWT.NONE -
for the end of a drag. SWT.DRAG.
SWT.HOME. SWT.END. SWT.ARROW_DOWN.
SWT.ARROW_UP. SWT.PAGE_DOWN.
SWT.PAGE_UP. widgetDefaultSelected is
not called.
So my suggestion is get your tooltip to appear and disappear is to check for the event.detail type.
public void widgetSelected(SelectionEvent event) {
tip.setVisible(event.detail != SWT.NONE);
}
scrollBar.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
if (e.detail == SWT.NONE) {
// end of drag
System.out.println("Drag end");
}
else if (e.detail == SWT.DRAG) {
// drag
System.out.println("Currently dragging");
}
}
});
Hope this will help you... But I can see a problem with mousewheel use that throws multiple drag end events...
Paul,
try using addMouseEvent method from a Scrollable object. For example:
Scrollable scrollable = scrollbar.getParent();
scrollable.addMouseListener(new MouseListener () {
void mouseDoubleClick(MouseEvent e) { ... }
void mouseDown(MouseEvent e) { ... }
void mouseUp(MouseEvent e) { ... }
});
Actually, I don't know if this approach will work. But, it's an attempt.
Good luck!