Java: Swing: User interface: Context menus and selection
The popup-trigger mouse event may also trigger a change in the selection, depending on LAF. How to be really sure one sees the proper selection state and not some temporary, incomplete state because the UIs MouseListener method is only executed afterwards?
Not really possible, but typically changes are handled in mousePressed.
Use SwingUtilities.invokeLater to handle all other mouse pressed/released listeners before looking at the selection.
Typically, a UI mouseListener that does something in response to a popup-trigger event should be considered evil because it violates some of the targets below.
Does the context menus refer to the selected items, the item under the mouse, both, ...?
Targets:
Accidental activation of the context menu shouldn't affect the selection (including lead/anchor).
In general, it should be possible to show context menus that refer to multiple items.
Possible strategy:
If no item is selected, the item under the mouse becomes (temporarily?, probably not) selected, and the popup refers to it.
If an item is selected, the popup refers to it.
If multiple items are selected, try to display the popup for them. If that is not possible (no common set of operations), remove from the selection those that hinder it, the lead selection item and the item under the mouse (both only if selected) last.
If a context cannot be provided for any of the selected items and the item under the mouse (if any) isn't selected (i.e. not in this set), select it if a popup can be displayed for it, then continue.
Scroll at least one of the items the context menu refers to into view.
Display the popup menu at the mouse location, maybe corrected so it isn't (at least obviously) outside of the screen (whatever that means with newer JDKs).
Afterwards, restore old selection, at least the shrunk part (Having to reselect them just because you happened to activate the context menus although maybe you didn't even intend to is annoying). Also restore anchor/lead items (which are, alas, implicitly changed by changing the selection). Maybe also scroll back (at least when the popup menu was cancelled).
By keyboard: suppose there is a "Show context menu" key.
If it is pressed and no item is selected, if there is a lead selection item isn't selected, select it, otherwise do nothing.
Otherwise narrow selection as above if necessary.
Scroll at least one of the items the context menu refers to into view.
Display the popup menu at the selection of (the first of?) these items, maybe corrected as above.
Afterwards, handle selection as above.
Mouse clicks outside the popup menu may try to change the selection (in addition to hiding the popup)?
One cannot just make the glass pane visible, that would possibly also "hide" the popup menu. IMO it should be possible to dismiss the popup menu (by mouse) without any side effects and without looking for a place where a click will (probably) do nothing.
(C) 2001-2009 Christian Kaufhold (swing@chka.de)