Bonjour,
Pour une IHM en SWING je voudrais faire un PopUp d'Help qui se déclanche sur via un clic droit sue l’icône Help.
Depuis la version J2SE 1.5 (Tiger) de JAVA, les Component SWING possèdent la méthode setComponentPopupMenu. Je voudrais utiliser cette méthode plutôt que la manière JAVA 4 avec les Listeners et le Popup.show.
Je précise que je débute en SWING.
Mon Problème est que je ne suis plus maître de l’événement déclencheur du PopUp. Par défaut dans mon application il ne se lance que sur le clic droit. Je voudrais qu’il se lance avec le clic gauche.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 /******************************************************************************************************************************************************************************************************************************************************************************************************* * Return a JPanel containing an Help icon * * @return the value to be displayed */ protected JPanel getHelp() { JPanel res = new JPanel(); JLabel iconHelp = new JLabel(getHelpIcon()); iconHelp.setComponentPopupMenu(getHelpPopup()); res.add(iconHelp); return res; } /******************************************************************************************************************************************************************************************************************************************************************************************************* * Create A JPopupMenu that shows up by clicking on the help icon The JPopup contains a JViewer which allows to display HMTL * * @return the JPopup */ protected JPopupMenu getHelpPopup() { String help = GenericUtils.getHelp(theProperty.getHelp(), theProperty.getId(), getLocale()); JPopupMenu res = new JPopupMenu(GenericUtils.getValue("label.help", getLocale())); JEditorPane viewer = new JEditorPane("text/html", help); viewer.setEditable(false); res.add(viewer); res.setInheritsPopupMenu(true); return res; }
http://www-128.ibm.com/developerwork...124/index.html
Il est indiqué :Sur un autre site : http://www.jguru.com/forums/view.jsp?EID=1239349 J’ai trouvé un post contradictoire :With Tiger, the triggering of the popup menu is now part of the UI definition of the component. And, if that component UI wants to have a triggering event like a key click, the changes stay focused within the UI definition, not in all the uses of that popup menu.
JDK 1.5 adds the setComponentPopupMenu method to JComponent, but if this method is used to add a popup menu to a JPanel, a right mouse click does nothing. Adding a do nothing MouseListener to the panel makes the popup come alive. This behaviour seems counter intuitive.
Is this a bug, or is there a another more logical way to indicate that the panel should handle popup events?
Using the setInheritsPopupMenu on a JComponent added to the panel does make that component display the inherited menu.
In the example that follows, only panelA shows the popup, because it is the one with the added empty MouseListener.
Pour déclancher l’apparition du PopUp non pas sur le clic droit mais sur le clic gauche.
Dois-je :
• Créer des Listener vides
• Changer L’UI ? Comment ?
• Autre?
Notez enfin qu’il existe un BUG JAVA lié à ce problème :
http://bugs.sun.com/bugdatabase/view...bug_id=6272233
Je ne pense pas que ça soit un problème car mon PopUp s’affiche.
Merci d’avance si vous avez une réponse ou un tutorial correct (pas trouvé) pour utiliser complètement cette nouvelle fonctionnalité de JAVA 5.
Damien
Partager