IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants Java Discussion :

Jtable + ToolTip dans les cellules


Sujet :

Composants Java

  1. #1
    Nouveau membre du Club
    Inscrit en
    Mai 2002
    Messages
    46
    Détails du profil
    Informations forums :
    Inscription : Mai 2002
    Messages : 46
    Points : 38
    Points
    38
    Par défaut [Resolu] Jtable + ToolTip dans les cellules
    Voici mon code :

    Le modèle de table :

    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
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
     
    import javax.swing.table.*;
     
    public class PlanningModel
        extends AbstractTableModel {
     
      String[] columnNames = {"Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi","Dimanche"};
     
      Object[][] data = null;
     
     
      public PlanningModel(int l, int c) {
        super();
        data = new Object[l][c];
      }
     
      public int getColumnCount() {
        return columnNames.length;
      }
     
      public Object getValueAt(int l, int c) {
        return data[l][c];
      }
     
      public void setValueAt(Object val, int l, int c) {
        data[l][c] = val;
      }
     
     public int getRowCount() {
        return data.length;
      }
     
      public String getColumnName(int c) {
        return columnNames[c];
      }
     
      public Class getColumnClass(int c) {
        try {
          return getValueAt(0, c).getClass();
        }
        catch (Exception ex) {
          return "".getClass();
        }
      }
     
      public boolean isCellEditable(int r, int c) {
          return false;
      }
     
    }
    Le TableCellRenderer :

    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
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
     
    import javax.swing.JLabel;
    import javax.swing.JTable;
    import javax.swing.table.TableCellRenderer;
    import java.awt.Color;
    import java.awt.Component;
    import javax.swing.JPanel;
    import jprojet.beans.PlanningBean;
     
     
    public class PlanningColorRenderer extends JPanel
                               implements TableCellRenderer {
     
        public PlanningColorRenderer() {
     
          setLayout(new com.borland.jbcl.layout.VerticalFlowLayout());   
          setOpaque(true);
          setBackground(Color.white);
        }
     
        public Component getTableCellRendererComponent(
                                JTable table, Object value, boolean selected, boolean focused, int row, int column)  {
     
          removeAll();
          invalidate();
     
          setFont(table.getFont());
     
          PlanningBean planningBean = null;
          try {
            planningBean = (PlanningBean) value;
          }
          catch (Exception ex) {
            ex.printStackTrace();
          }
     
          if(planningBean != null){
            String jour = planningBean.getJour();
            if(jour != null && jour.length() > 0)
              setData(jour, JLabel.RIGHT, new Color(184,184,184), null);
     
            String[] rdv = planningBean.getRdv();
            String[] datailRdv = planningBean.getDetailRdv();
            if(rdv != null && rdv.length > 0){
              for(int r=0; r<rdv.length; r++){
                String detail = "";
                if(datailRdv != null && datailRdv.length > 0)
                  detail = datailRdv[r];
     
                String rendezVous = rdv[r];
                setData(rendezVous, JLabel.LEFT, Color.WHITE, detail);
              }
            }
     
     
          }
     
     
          return this;
        }
     
        private void setData(String text, int align, Color color, String toolTip){
          JLabel textLabel = new JLabel();
          textLabel.setOpaque(true);      
          textLabel.setText(text);
          textLabel.setHorizontalAlignment(align);
          textLabel.setBackground(color);
          textLabel.setBorder(null);
          if(toolTip != null && toolTip.length() > 0){
            textLabel.setToolTipText(toolTip);
          }
          add(textLabel);
        }
    }
    Une partie du code pour afficher la jtable :

    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
     
    PlanningModel planningModel = new PlanningModel(6, 7);
     
    JTable jTable1 = new JTable(planningModel);    
    jScrollPane1.getViewport().add(jTable1);
     
    PlanningColorRenderer planningColorRenderer = new PlanningColorRenderer();
    jTable1.setDefaultRenderer(PlanningBean.class, planningColorRenderer);
    jTable1.setRowSelectionAllowed(false);
     
        String[] rdv = {"rdv1","rdv2","rdv3","rdv4"};
        String[] rdvDetail = {"9h-10h, Paris","11h-12h, Paris","14h-18h, Paris","18h-19h, Paris"};
     
     
        for (int row = 0; row < 6; row++) {
          for (int column = 0; column < 7; column++) {
     
               PlanningBean planningBean = new PlanningBean();
                planningBean.setJour(day + "");
                planningBean.setRdv(rdv);
                planningBean.setDetailRdv(rdvDetail);            
                jTable1.setValueAt(planningBean, row, column);
     
          }
        }
    Le problème :

    Dans ma classe PlanningColorRenderer, je demande d'afficher les ToolTip, mais ils ne s'afficent pas.
    Quelqu'un à une idée ?

    Merci de votre aide

  2. #2
    Membre habitué
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    104
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 104
    Points : 140
    Points
    140
    Par défaut
    Essaie de déplacer l'appel de setToolTipText dans la méthode getTableCellRendererComponent.
    Si je me souviens bien, c'est le même objet CellRenderer qui est utilisé pour l'affichage de toutes les cellules.

  3. #3
    Nouveau membre du Club
    Inscrit en
    Mai 2002
    Messages
    46
    Détails du profil
    Informations forums :
    Inscription : Mai 2002
    Messages : 46
    Points : 38
    Points
    38
    Par défaut
    Il est deja appelé par getTableCellRendererComponent.
    ou je n'ai pas compris ce que tu me dis

  4. #4
    Membre habitué
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    104
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 104
    Points : 140
    Points
    140
    Par défaut
    Tu as bien compris... c'est moi qui n'ai pas lu ton code assez attentivement.

  5. #5
    Membre expérimenté Avatar de herve91
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    1 282
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 282
    Points : 1 608
    Points
    1 608
    Par défaut
    Ton tooltiptext est placé sur un JLabel alors que le renderer renvoie un JPanel, qui par défaut n'émet pas de tooltip.
    Soit ton renderer renvoie le JLabel en lieu et place du JPanel (d'ailleurs ce dernier ne contient que le JLabel, donc ça n'a pas d'intérêt de mettre le JLabel dans un JPanel), soit tu fais en sorte que le JPanel puisse émettre des tooltip :
    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
     
    public PlanningColorRenderer() { 
     
          setLayout(new com.borland.jbcl.layout.VerticalFlowLayout());    
          setOpaque(true); 
          setBackground(Color.white); 
          ToolTipManager.sharedInstance().registerComponent(this); 
        } 
     
        private void setData(String text, int align, Color color, String toolTip){ 
          ...
          if(toolTip != null && toolTip.length() > 0){ 
            setToolTipText(toolTip); 
          }
          ...

  6. #6
    Nouveau membre du Club
    Inscrit en
    Mai 2002
    Messages
    46
    Détails du profil
    Informations forums :
    Inscription : Mai 2002
    Messages : 46
    Points : 38
    Points
    38
    Par défaut
    Merci, ca fonctionne.

    Peux tu être un peu plus explicite sur :

    (d'ailleurs ce dernier ne contient que le JLabel, donc ça n'a pas d'intérêt de mettre le JLabel dans un JPanel)
    Car en faite mon panel contient plusieurs Jlabel différents

    Merci

  7. #7
    Membre expérimenté Avatar de herve91
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    1 282
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 282
    Points : 1 608
    Points
    1 608
    Par défaut
    au temps pour moi, je n'avais pas fait attention à la boucle autour de l'appel à la méthode setData(). Je l'aurais appelé autrement pour éviter l'ambiguité du nom, addData() par exemple.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [JTable] JRadioButton dans les cellules
    Par Takis dans le forum Composants
    Réponses: 4
    Dernier message: 14/04/2008, 13h54
  2. Réponses: 3
    Dernier message: 04/04/2007, 14h18
  3. JTable avec des JPanel dans les cellule
    Par pigpen dans le forum Composants
    Réponses: 11
    Dernier message: 13/04/2006, 19h58
  4. Jtable et multi-lignes dans les cellules
    Par tuxor dans le forum Composants
    Réponses: 2
    Dernier message: 19/11/2005, 07h32
  5. [JTable] centrer les donnees dans les cellules
    Par cmoa59 dans le forum Composants
    Réponses: 5
    Dernier message: 20/05/2005, 11h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo