Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35580 > unrolled thread

Why must look and feel wipe out my mouse events?

Started byEric Douglas <e.d.programmer@gmail.com>
First post2016-09-09 10:38 -0700
Last post2016-09-11 00:14 +0200
Articles 6 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Why must look and feel wipe out my mouse events? Eric Douglas <e.d.programmer@gmail.com> - 2016-09-09 10:38 -0700
    Re: Why must look and feel wipe out my mouse events? Eric Douglas <e.d.programmer@gmail.com> - 2016-09-09 11:09 -0700
      Re: Why must look and feel wipe out my mouse events? Eric Douglas <e.d.programmer@gmail.com> - 2016-09-09 11:46 -0700
        Re: Why must look and feel wipe out my mouse events? j4n bur53 <janburse@fastmail.fm> - 2016-09-10 15:27 +0200
          Re: Why must look and feel wipe out my mouse events? Andreas Leitgeb <avl@logic.at> - 2016-09-10 14:06 +0000
            Re: Why must look and feel wipe out my mouse events? j4n bur53 <janburse@fastmail.fm> - 2016-09-11 00:14 +0200

#35580 — Why must look and feel wipe out my mouse events?

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-09-09 10:38 -0700
SubjectWhy must look and feel wipe out my mouse events?
Message-ID<b832ea9e-f255-4fa9-95ff-e8a57a86f988@googlegroups.com>
It took me awhile to figure out this is what causes it.  I write custom extensions of these controls and thought it was something I was doing with the controls themselves, but the look and feel makes my events disappear!

Try the following program.  Right click on a tree node and you'll see it registers the mouse was pressed.  Then while the popup menu is still visible right click the other tree node and you'll see it does not register the mouse button pressed.  Change the getSystemLookAndFeelClassName() to getCrossPlatformLookAndFeelClassName() and it does show the mouse was pressed both times!

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

public class TestMouse {
     public static void main(final String[] args) {

          EventQueue.invokeLater(new Runnable() {

               @Override
               public void run() {
                    try {
                         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
                         e.printStackTrace();
                    }
                    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
                    DefaultMutableTreeNode vegetableNode = new DefaultMutableTreeNode("Vegetables");
                    DefaultMutableTreeNode fruitNode = new DefaultMutableTreeNode("Fruits");
                    root.add(vegetableNode);
                    root.add(fruitNode);
                    JTree tree = new JTree(root);
                    tree.addMouseListener(new MouseAdapter() {
                         @Override
                         public void mousePressed(final MouseEvent e) {
                              System.out.println("pressed");
                              final int selRow = tree.getRowForLocation(e.getX(), e.getY());
                              final TreePath p = tree.getPathForLocation(e.getX(), e.getY());
                              if (p != null) {
                                   if (selRow != -1) {
                                        if (e.getClickCount() == 1) {
                                             tree.setSelectionPath(p);
                                        }
                                   }
                              }
                         }
                    });
                    JPopupMenu popup = new JPopupMenu();
                    popup.add(new JMenuItem("Test One"));
                    popup.add(new JMenuItem("Test Two"));
                    tree.setComponentPopupMenu(popup);
                    JFrame win = new JFrame();
                    win.add(tree);
                    win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    win.pack();
                    win.setVisible(true);
               }
          });
     }

}

[toc] | [next] | [standalone]


#35581

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-09-09 11:09 -0700
Message-ID<3ef9903c-46a5-4779-bb74-7b65c9694a11@googlegroups.com>
In reply to#35580
On Friday, September 9, 2016 at 1:38:26 PM UTC-4, Eric Douglas wrote:
> It took me awhile to figure out this is what causes it.  I write custom extensions of these controls and thought it was something I was doing with the controls themselves, but the look and feel makes my events disappear!
> 
> Try the following program.  Right click on a tree node and you'll see it registers the mouse was pressed.  Then while the popup menu is still visible right click the other tree node and you'll see it does not register the mouse button pressed.  Change the getSystemLookAndFeelClassName() to getCrossPlatformLookAndFeelClassName() and it does show the mouse was pressed both times!
> 

I tested this with all the values of getInstalledLookAndFeels().  Metal does register those mouse click events.  Nimbus, Motif, Windows, and Windows Classic do not.

[toc] | [prev] | [next] | [standalone]


#35582

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-09-09 11:46 -0700
Message-ID<72a246c3-4c5c-4c73-aa57-73eafe141fb6@googlegroups.com>
In reply to#35581
On Friday, September 9, 2016 at 2:10:05 PM UTC-4, Eric Douglas wrote:
> 
> I tested this with all the values of getInstalledLookAndFeels().  Metal does register those mouse click events.  Nimbus, Motif, Windows, and Windows Classic do not.

Aha, seems dumb it won't tell the JTree you clicked on it if the popup window is visible but I found the workaround.
http://stackoverflow.com/questions/23505541/swing-mouse-click-event-for-jmenu-and-jpopupmenu

Adding that line makes it register.
UIManager.put("PopupMenu.consumeEventOnClose", false);

Now, as the fix sounds like a hack, is there any reason I wouldn't want it not consuming an event, and is there any chance something else in the application needs it to consume...

[toc] | [prev] | [next] | [standalone]


#35585

Fromj4n bur53 <janburse@fastmail.fm>
Date2016-09-10 15:27 +0200
Message-ID<nr11nn$6f9$1@news.albasani.net>
In reply to#35582
If your is over the nuclear war start button,
and click it beacause you only want to close
a popup, its better the event doesnt permeate.


Eric Douglas schrieb:
> On Friday, September 9, 2016 at 2:10:05 PM UTC-4, Eric Douglas wrote:
>>
>> I tested this with all the values of getInstalledLookAndFeels().  Metal does register those mouse click events.  Nimbus, Motif, Windows, and Windows Classic do not.
>
> Aha, seems dumb it won't tell the JTree you clicked on it if the popup window is visible but I found the workaround.
> http://stackoverflow.com/questions/23505541/swing-mouse-click-event-for-jmenu-and-jpopupmenu
>
> Adding that line makes it register.
> UIManager.put("PopupMenu.consumeEventOnClose", false);
>
> Now, as the fix sounds like a hack, is there any reason I wouldn't want it not consuming an event, and is there any chance something else in the application needs it to consume...
>

[toc] | [prev] | [next] | [standalone]


#35586

FromAndreas Leitgeb <avl@logic.at>
Date2016-09-10 14:06 +0000
Message-ID<slrnnt84rb.456.avl@login.logic.tuwien.ac.at>
In reply to#35585
j4n bur53 <janburse@fastmail.fm> wrote:
> If your [mouse] is over the nuclear war start button,
> and click it beacause you only want to close
> a popup, its better the event doesnt permeate.

I'd try to avoid these situations, even when these events don't permeate,
because of the risk of bouncy mouse buttons (German: "Tastenprellen")

[toc] | [prev] | [next] | [standalone]


#35587

Fromj4n bur53 <janburse@fastmail.fm>
Date2016-09-11 00:14 +0200
Message-ID<nr20jm$vne$1@news.albasani.net>
In reply to#35586
Andreas Leitgeb schrieb:
> j4n bur53<janburse@fastmail.fm>  wrote:
>> >If your [mouse] is over the nuclear war start button,
>> >and click it beacause you only want to close
>> >a popup, its better the event doesnt permeate.

> I'd try to avoid these situations, even when these events don't permeate,
> because of the risk of bouncy mouse buttons (German: "Tastenprellen")

For this reason I recently trashed a trust
mouse (*), and went with logitech again.

(*)
http://www.trust.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web