Bonjour a tous,
comment je peux ajouter une image a une colonne dans un jtable.
quelqu'un peut m'expliquer.
merci
Version imprimable
Bonjour a tous,
comment je peux ajouter une image a une colonne dans un jtable.
quelqu'un peut m'expliquer.
merci
En utilisant un JTable Cell Renderer:
http://download.oracle.com/javase/tu...nts/table.html
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
merci à l'avance:cry:Code:
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 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); } }
Dans la methode 'getTableCellRendererComponent', tu dois tester ton parametre "value", qui doit etre de type Object[] (tu l'a ajouté avec addRow dans le TableModel) et donc tu pourras mettre une icone differente suivant le resultat ;)
Bonjour ,
si j'applique ce code
alors comment je modifie la classe principaleCode:
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 class ImageRenderer extends DefaultTableCellRenderer { public static final String CONNECTED_PICTURE = "images.jpg"; public static final String DISCONNECTED_PICTURE = "planète.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(); } }
mais il ne m'affiche pas l'image seulement le chemin de l'image,comment je peux corriger le problèmeCode:
1
2 ImageRenderer v= new ImageRenderer(); scan[2]=v.getIconConnected();
merci
Pour un element de reponse, voici ton cellrenderer:
Pour gérer encore mieux tes informations, je te propose de créer un tableModel qui gérera tes tableaux:Code:
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 public class ImageRenderer extends DefaultTableCellRenderer { public static final String CONNECTED_PICTURE = "images.jpg"; public static final String DISCONNECTED_PICTURE = "planète.jpg"; protected JLabel labelDescription = new JLabel(); protected ImageIcon iconConnected = new ImageIcon(getClass().getResource( CONNECTED_PICTURE)); protected ImageIcon iconDisconnected = new ImageIcon(getClass().getResource( DISCONNECTED_PICTURE)); public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { System.out.println(value); String description = (String) value; labelDescription.setText(description); if ("connected".equals(description)) { labelDescription.setIcon(iconConnected); } else if ("disconnected".equals(description)) { labelDescription.setIcon(iconDisconnected); } return labelDescription; } }
Ensuite, pour tester tout ca, une petite JFrame:Code:
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 public class PersoTableModel extends AbstractTableModel { Vector<Object[]> v = new Vector<Object[]>(); public int getColumnCount() { return 3; } public int getRowCount() { return v.size(); } public Object getValueAt(int rowIndex, int columnIndex) { Object[] o = (v.get(rowIndex)); return o[columnIndex]; } public void addScan(Object[] o) { v.add(o); } }
Si tu as des questions, n'hesite pas ;)Code:
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 public class Test extends JFrame { public Test() { setSize(new Dimension(300, 300)); Object[] scan = new Object[3]; JTable table = new JTable(); PersoTableModel model = new PersoTableModel(); table.setModel(model); table.getColumnModel().getColumn(2).setCellRenderer( new ImageRenderer()); String s = "ok"; // pingResult :resultat de ping if (s.contains("Impossible de joindre l'h�te de destination")) { System.out.println("ne ping pas"); scan[0] = "ip"; scan[1] = false; scan[2] = "disconnected"; model.addScan(scan); } else { System.out.println("ping correctement"); scan[0] = "ip"; scan[1] = true; scan[2] = "connected"; model.addScan(scan); } setLayout(new BorderLayout()); add(table); } public static void main(String[] args) { new Test().setVisible(true); } }