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 ComboBox

Astuces de l’Infobrol (Java)Article publié le 21/01/2008 13:54:34


Suite à l'astuce qui permettait d'afficher plusieurs colonnes dans un ComboBox, il existe une alternative intéressante : afficher un ToolTip avec la description pour chaque élément.

Pour ne pas créer à chaque fois un nouveau ToolTip, nous utiliserons
  1. ToolTipManager.sharedInstance().registerComponent(this);


Nous allons afficher dans le ToolTip le code en gras et la description en italique, avec des espaces insécables en début et en fin de chaîne pour que le ToolTip présente un padding horizontal.

  1. package be.mil.cccis.atdl3.util.renderer;
  2.  
  3. import java.awt.Component;
  4.  
  5. import javax.swing.JLabel;
  6. import javax.swing.JList;
  7. import javax.swing.ListCellRenderer;
  8. import javax.swing.ToolTipManager;
  9.  
  10. import be.mil.cccis.atdl3.domain.generic.Catalog;
  11. /**
  12.  * Provides custom display for ‹code›be.mil.cccis.atdl3.domain.generic.Catalog‹/code›.
  13.  * Paints it contents in two columns CODE and DESCRIPTION.
  14.  * @author gaudry.s
  15.  *
  16.  */
  17. public class CatalogComboBoxRenderer extends JLabel implements ListCellRenderer {
  18.  
  19. /**
  20. *
  21. */
  22. private static final long serialVersionUID = 162343178097966067L;
  23. /**
  24. * Creates a new renderer
  25. */
  26. public CatalogComboBoxRenderer()
  27. {
  28. setOpaque(true);
  29. setHorizontalAlignment(LEFT);
  30. setVerticalAlignment(CENTER);
  31. }
  32.  
  33. /* (non-Javadoc)
  34. * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
  35. */
  36. public Component getListCellRendererComponent(JList list, Object value,
  37. int index, boolean isSelected, boolean cellHasFocus) {
  38. if(value==null){
  39. setText("");
  40. }
  41. else{
  42. if(value instanceof Catalog){
  43. Catalog item = (Catalog)value;
  44. String description = (item.getDescription()!=null)?item.getDescription():"[No description]";
  45. setText( "‹html›‹body›"+item.getCode()+"‹/body›‹/html›" );
  46. setToolTipText( "‹html›‹body›  ‹b›"+item.getCode() + "‹/b› ‹i›" + description+"‹/i›  ‹/body›‹/html›");
  47. ToolTipManager.sharedInstance().registerComponent(this);
  48. }
  49. else setText("‹html›‹body›"+value.toString()+"‹/body›‹/html›");
  50. }
  51. setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
  52. setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
  53. return this;
  54. }
  55.  
  56. }


En plus, comme dans cette application certains appels à toString retournent du code HTML (Attention Et là je ne suis pas d'accord du tout de laisser au modèle la responsabilité d'une certaine logique de présentation, mais c'est ce qui arrive quand on travaille à plusieurs en projet), le Renderer enrobe le texte aussi par des balises HTML.

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-429.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.