Voici mon code :
Le modèle de table :
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 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; } }
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
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); } }
Le problème :
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); } }
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
Partager