Results 1 to 13 of 13
  1. #1
    Taiko is offline Member
    Join Date
    Jan 2010
    Location
    Opava, Czech Republic
    Posts
    14
    Rep Power
    0

    Question 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  3. #3
    Taiko is offline Member
    Join Date
    Jan 2010
    Location
    Opava, Czech Republic
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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
    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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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).

  5. #5
    Taiko is offline Member
    Join Date
    Jan 2010
    Location
    Opava, Czech Republic
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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).
    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:

    Java Code:
     
    SwingUtilities.getDeepestComponentAt(Component parent, int x, int y)
    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?

  6. #6
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    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.

  7. #7
    Taiko is offline Member
    Join Date
    Jan 2010
    Location
    Opava, Czech Republic
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by zweibieren View Post
    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.
    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.

  8. #8
    Taiko is offline Member
    Join Date
    Jan 2010
    Location
    Opava, Czech Republic
    Posts
    14
    Rep Power
    0

    Default

    :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.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  10. #10
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    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:
    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"
    	}
    Then the constructor for the JInternalFrame must override setSelected to veto the operation if the right mouse button is down:
    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);
    		}
    	};
    Attached Files Attached Files

  11. #11
    Taiko is offline Member
    Join Date
    Jan 2010
    Location
    Opava, Czech Republic
    Posts
    14
    Rep Power
    0

    Thumbs up

    Wow :eek:

    Great work ;)

  12. #12
    hrodriguezve is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default

    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,

  13. #13
    Martino is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    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:

    Java Code:
    if (e.getModifiers() == 16) // The left mouse button is clicked.
    {
          // Source code...
    }
    Of course, you can also use a switch-case statement.

Similar Threads

  1. looping and filtering
    By javafanatic in forum New To Java
    Replies: 14
    Last Post: 02-09-2010, 09:48 AM
  2. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 04:34 AM
  3. [SOLVED] Mouse event in JTabbed Pane
    By javanewbie in forum AWT / Swing
    Replies: 6
    Last Post: 06-10-2009, 08:50 AM
  4. Mouse Event + Image Thresholding
    By ojmayolebron in forum AWT / Swing
    Replies: 0
    Last Post: 03-27-2009, 12:17 AM
  5. filtering data in jsf
    By bbq in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 07-04-2007, 08:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •