Results 1 to 13 of 13
Thread: Mouse event filtering?
- 01-16-2010, 08:55 PM #1
Member
- Join Date
- Jan 2010
- Location
- Opava, Czech Republic
- Posts
- 14
- Rep Power
- 0
Mouse event filtering?
My problem is, that I don't want some components to react on right mouse button click.
For example: I have a few JInternalFrames on my JDesktopPane and they are shown in different "layers": one is at the back, one is at the front, others are between them (some behind another) :confused: And when I click on the visible part of some "hiding" frame, it get to front. And I want to do this only by left mouse button click, not by right mouse button click :(
And another example: resizing. I want to resize frames only by left mouse button, but it reacts also on right mouse button :(
I hope, that you understand what I'm trying to say, even from my confused explanation and bad english :o
EDIT: Here is a snapshot: http://img259.imageshack.us/img259/2795/problemx.jpg
-> see the cursor on the image
-> when I click (or just press) the right mouse button, the frame (JInternalFrame) gets to the front
And I want every component to ignore the right mouse button action.Last edited by Taiko; 01-17-2010 at 09:01 AM. Reason: I have added a snapshot.
- 01-17-2010, 12:21 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
If you disable a component it won't respond to any events. See the JComponent.setEnabled(boolean enabled) method for a detailed explanation.
kind regards,
Jos
- 01-17-2010, 12:47 PM #3
Member
- Join Date
- Jan 2010
- Location
- Opava, Czech Republic
- Posts
- 14
- Rep Power
- 0
But I want theese components to respond to most of events. The only events, that I don't want, are mouse events evoked by right-mouse-button click.
So, I think, that I have to override some component listener, but I don't know, how to differentiate and filter out only right-mouse-button mouse events.
Or, I have to make theese components "deaf" (ignoring) to mouse events evoked by right-mouse-button.Last edited by Taiko; 01-17-2010 at 12:52 PM. Reason: mistakes
-
I don't know the official way to do this, but what if you turned on the glass pane, and had it pass mouse events through to the underlying components but only if they are left clicks. This may work for the GUI itself, but I doubt that it would work for app resizing since I believe that that's an OS function (though I could be wrong).
- 01-17-2010, 02:54 PM #5
Member
- Join Date
- Jan 2010
- Location
- Opava, Czech Republic
- Posts
- 14
- Rep Power
- 0
I tried to use GlassPane, but had some problems with it. Concretely: with redispatching mouse events to underlying components. It wasn't problem to redispatch events from MouseListener, but it was problem to redispatch events from MouseMotionListener :( Because, I tried to redispatch mouse events to components get by:
But, it means, that (in my case) the MouseMotion events worked correctly only when the cursor was pointing on the component {but, for example, when you pull a scrollbar (MOUSE_DRAGGED event), it moves, until you release the mouse button - and it doesn't matter "where are you moving the cursor"}. Is it clear from my confused explanation?Java Code:SwingUtilities.getDeepestComponentAt(Component parent, int x, int y)
- 01-17-2010, 03:09 PM #6
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
A recent correspondent had success with AWTEventListener as sugggested in an old thread.
But please note that you are messing with the look and feel.
This sort of tinkering can cause users to mistrust their instincts
and have some fears in their use of your application.Last edited by zweibieren; 01-17-2010 at 03:11 PM.
- 01-17-2010, 03:57 PM #7
Member
- Join Date
- Jan 2010
- Location
- Opava, Czech Republic
- Posts
- 14
- Rep Power
- 0
You are reffering me to my thread :( This misunderstanding has to be because of my bad english :(
Yes, I know how to "catch" every right-button-click on my application and use it somehow, but I don't know how to handle, which components can respond to this event and which components can't. I can use the AWTEventListener only to get all the events. But except AWTEventListener also another Listeners will get the events and will respond to them (and that is my problem).
My english is bad and I think, I'm not able to write a short small runable example. So, I am looking for some volunteer. Can I send to some volunteer's email my unfinished project to simply see and try, what I want? I think, that it can save a lot of time to us. Please, contact me at Taiko@email.cz
And believe me, in my case, this tinkering is to improve the look and feel of my application.
- 01-17-2010, 08:10 PM #8
Member
- Join Date
- Jan 2010
- Location
- Opava, Czech Republic
- Posts
- 14
- Rep Power
- 0
:mad::mad::mad::mad::mad:
I am stupid!
The solution of my problem is very simple, it is method consume() of class InputEvent. So, as zweibieren wrote, AWTEventListener works fine. The only problem is somewhere in my head :):):)
Java Code:class MyAlmightyListener implements AWTEventListener { private MouseEvent event; public void eventDispatched(AWTEvent e) { event = (MouseEvent) e; if (event.getID() == MouseEvent.MOUSE_PRESSED) { if (event.getButton() == 3) { [B]event.consume();[/B] } } } }Java Code:long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK; Toolkit.getDefaultToolkit() .addAWTEventListener( new MyAlmightyListener(), eventMask);
Last edited by Taiko; 01-17-2010 at 08:14 PM.
-
- 01-18-2010, 08:33 PM #10
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
Meanwhile, back in our subterranean labs hidden far beneath a bucolic countryside,
I discovered how to more precisely disable the right button
for just those events where we want it disabled. Attached.
There are two facets to the solution.
-- To disable the right mouse button for border events (resize, move, buttons)
it is necessary to override the look-and-feel specific version of BorderLayout.
It is too bad that a more general solution is not possible.
The code in the constructor is
Java Code:internalFrame.setUI(new MyWIFUI(internalFrame)); ... static class MyWIFUI extends WindowsInternalFrameUI { public MyWIFUI(JInternalFrame w) { super(w); } @Override protected MouseInputAdapter createBorderListener(JInternalFrame w) { return new MyBorderListener(); } class MyBorderListener extends BasicInternalFrameUI.BorderListener implements MouseListener, MouseMotionListener { MyBorderListener() { super(); } boolean okay(MouseEvent e) { return 0 == (e.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK); } @Override public void mouseDragged(MouseEvent e) { if (okay(e)) super.mouseDragged(e); } // and so on for the other MouseEvents
-- To disable activating a window by clicking,
one must counteract a kludge in BasicLookAndFell with a kludge of ones own.
First we have to keep track of the state of the right mouse button:
Then the constructor for the JInternalFrame must override setSelected to veto the operation if the right mouse button is down:Java Code:public class DesktopDemo extends JDesktopPane implements ActionListener { // kludge to disable setSelect() from right mouse button boolean rightMouseDown = false; public DesktopDemo() { super(); Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() { public void eventDispatched(AWTEvent event) { MouseEvent e = (MouseEvent)event; rightMouseDown = 0 != ////// set rightMouseDown (e.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK); } }, AWTEvent.MOUSE_EVENT_MASK); createSubwindow(); //create first "subwindow" }
Java Code:/**Create a new subwindow and insert it in the desktop. */ protected void createSubwindow() { JInternalFrame subwin = new JInternalFrame( "Document #" + (++subCount), // title true, //resizable true, //closable true, //maximizable true) //iconifiable { public void setSelected(boolean selected) throws PropertyVetoException { if (selected && rightMouseDown) throw new PropertyVetoException("right mouse is down", null); else super.setSelected(selected); } };
- 01-18-2010, 09:52 PM #11
Member
- Join Date
- Jan 2010
- Location
- Opava, Czech Republic
- Posts
- 14
- Rep Power
- 0
Wow :eek:
Great work ;)
- 08-08-2011, 10:31 PM #12
Member
- Join Date
- Aug 2011
- Posts
- 1
- Rep Power
- 0
The solution it's too easy. You must set the ComponentPopupMenu to null to avoid that this appears.
For more details, you can search on Java Api
JComponent.setComponentPopupMenu
Best regards,
- 08-10-2011, 02:00 PM #13
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
Hi,
You can make a list of:
System.out.println(e.getModifiers());
in the MouseEvent handler.
Each mouse button press generates a different integer.
The integers you want to pass through, can be used for filtering in an if statement, like:
Of course, you can also use a switch-case statement.Java Code:if (e.getModifiers() == 16) // The left mouse button is clicked. { // Source code... }
Similar Threads
-
looping and filtering
By javafanatic in forum New To JavaReplies: 14Last Post: 02-09-2010, 09:48 AM -
Mouse Listener for mouse floating over object?
By Krooger in forum AWT / SwingReplies: 1Last Post: 11-18-2009, 04:34 AM -
[SOLVED] Mouse event in JTabbed Pane
By javanewbie in forum AWT / SwingReplies: 6Last Post: 06-10-2009, 08:50 AM -
Mouse Event + Image Thresholding
By ojmayolebron in forum AWT / SwingReplies: 0Last Post: 03-27-2009, 12:17 AM -
filtering data in jsf
By bbq in forum JavaServer Faces (JSF)Replies: 2Last Post: 07-04-2007, 08:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks