Affichage d'une image dans un JTable

isgt Messages postés 43 Date d'inscription samedi 29 mars 2008 Statut Membre Dernière intervention 15 mai 2012 - 27 janv. 2011 à 02:33
isgt Messages postés 43 Date d'inscription samedi 29 mars 2008 Statut Membre Dernière intervention 15 mai 2012 - 27 janv. 2011 à 11:36
bonsoir,
j'ai utilisé le jTable et cell renderer mais mon probléme que le même image s'affiche a chaque ligne alors que je veux faire un if pour tester si connecte une image si nn connecté une aure image .comment je résout le problème
class ImageRenderer extends DefaultTableCellRenderer {
  JLabel lbl = new JLabel();

  ImageIcon icon = new ImageIcon(getClass().getResource("images.jpg"));

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
      boolean hasFocus, int row, int column) {
    lbl.setText((String) value);
    lbl.setIcon(icon);
    return lbl;
  }
 Object [] scan=new Object[3];

 TableModel modele=jTable1.getModel();

//pingResult :resultat de ping

 if(pingResult.contains("Impossible de joindre l'h�te de destination"))
             {
                        System.out.println("ne ping pas");
            scan[0]=ip;
            scan[1]=false;
         
            ((DefaultTableModel)modele).addRow(scan);
            jTable1.getColumnModel().getColumn(2).setCellRenderer(new ImageRenderer());

               jTable1.setModel(modele);
               
                jTable1.repaint(0);
          
             }

             else
{
                        System.out.println("ping correctement");

            scan[0]=ip;
            scan[1]=true;
            ((DefaultTableModel)modele).addRow(scan);
 
               jTable1.setModel(modele);
                jTable1.repaint(0);
                
} }


merci à l'avance

2 réponses

cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
27 janv. 2011 à 07:58
Salut,

C'est dans ton CellRenderer de faire ce test, et d'appliquer l'image qu'il faut en conséquence, par exemple :

class ImageRenderer extends DefaultTableCellRenderer {
  public static final String CONNECTED_PICTURE = "imageConnecte.jpg";
  public static final String DISCONNECTED_PICTURE = "imageDeconnecte.jpg";

  protected JLabel description;
  protected ImageIcon iconConnected;
  protected ImageIcon iconDisconnected;

  protected JLabel getDescription() {
    if (description == null) {
      description = new JLabel();
    }
    return description;
  }

  protected ImageIcon getIconConnected() {
    if (iconConnected == null) {
      iconConnected = new ImageIcon(getClass().getResource(CONNECTED_PICTURE));
    }
    return iconConnected;
  }

  protected ImageIcon getIconDisconnected() {
    if (iconDisconnected == null) {
      iconDisconnected = new ImageIcon(getClass().getResource(DISCONNECTED_PICTURE));
    }
    return iconDisconnected;
  }

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    String description = (String) value;
    getDescription().setText(description);
    if ("connected".equals(description)) {
      getDescription().setIcon(getIconConnected());
    } else if ("disconnected".equals(description)) {
      getDescription().setIcon(getIconDisconnected());      
    }
    return getDescription();
  }
}
 

______________________________________

AVANT de poster votre message, veuillez lire, comprendre, et appliquer notre réglement
1
isgt Messages postés 43 Date d'inscription samedi 29 mars 2008 Statut Membre Dernière intervention 15 mai 2012
27 janv. 2011 à 11:36
Bonjour,
merci pour la réponse mais j'ai pas compris comment tester j'ai fait
 
ImageRenderer v= new ImageRenderer();
scan[2]=v.getIconConnected();


il m'affiche le chemin de l'image
merci
0
Rejoignez-nous