Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35580
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2016-09-09 10:38 -0700 |
| Message-ID | <b832ea9e-f255-4fa9-95ff-e8a57a86f988@googlegroups.com> (permalink) |
| Subject | Why must look and feel wipe out my mouse events? |
| From | Eric Douglas <e.d.programmer@gmail.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);
}
});
}
}
Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread
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
csiph-web