No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Vous devez être membre et vous identifier pour publier un article.
Les visiteurs peuvent toutefois commenter chaque article par une réponse.

[Swing] Tooltip sur les éléments d’un JComboBox

Astuces de l’Infobrol (Java)Article publié le 20/10/2008 13:56:15


Pour placer un ToolTip sur chaque élément d’une boîte de sélection (JComboBox), nous allons passer par un Renderer.
Quand nous désirons sélectionner un élément d’un JComboBox, ce dernier utilise une JList pour afficher les choix.

A l’aide d’un renderer, nous pouvons spécifier comment afficher chaque cellule de la liste, et par la même occasion afficher une bulle d’aide (ToolTip).

Le renderer



  1. import java.awt.Component;
  2.  
  3. import javax.swing.JLabel;
  4. import javax.swing.JList;
  5. import javax.swing.ListCellRenderer;
  6. import javax.swing.ToolTipManager;
  7.  
  8. import eu.mil.meat.domain.generic.Catalog;
  9. import eu.mil.meat.domain.generic.LogicalDeletionOnly;
  10.  
  11. public class CatalogComboBoxRenderer extends JLabel implements ListCellRenderer {
  12.  
  13. public CatalogComboBoxRenderer() {
  14. setOpaque(true);
  15. setHorizontalAlignment(LEFT);
  16. setVerticalAlignment(CENTER);
  17. }
  18.  
  19. public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
  20. boolean cellHasFocus) {
  21. if (value == null) {
  22. setText("");
  23. } else {
  24. String text = "‹html›‹body›" + value.toString() + "‹/body›‹/html›";
  25. if (value instanceof Catalog) {
  26. Catalog item = (Catalog) value;
  27. String description = (item.getDescription() != null) ? item.getDescription() : "[No description]";
  28. String toolTip = "‹html›‹body›  ‹b›" + item.getCode() + "‹/b› ‹i›" + description
  29. + "‹/i›  ‹/body›‹/html›";
  30. setToolTipText(toolTip);
  31. // forces tooltip staying on top
  32. ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);
  33. list.setOpaque(true);
  34. }
  35. if (value instanceof LogicalDeletionOnly) {
  36. LogicalDeletionOnly item = (LogicalDeletionOnly)value;
  37. text = item.isInUse() ? text : "‹html›‹strike›" + item + "‹/strike›‹/html›";
  38. }
  39. setText(text);
  40. }
  41. setBackground( isSelected ? list.getSelectionBackground() : list.getBackground());
  42. setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
  43. return this;
  44. }
  45.  
  46. }


Application



Nous pouvons décorer le JComboBox de la manière suivante :
  1. myComboBox.setRenderer(new CatalogComboBoxRenderer());


Le ToolTip du JComboBox s’affiche en dessous de la JListe


Pour pallier à ce problème, nous utilisons la ligne suivante :
  1. ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);


Avatar :: Steph Un article de Steph

Source : indéterminée


Sélection, tri et recherche d'articles
FILTRER :
TRIER :1er critère : 2e critère :
CHERCHER : Dans les titres Dans le contenu


[Afficher les liens en fonction des critères du formulaire ci-dessus]

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 13/09/2004, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/ast-rf-444.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.