salut
j'ai créé une classe Model qui me permet de créer ma table et j'ai créé une classe2 qui colorie ma table jusqu'ici ça marche bien mai quand j'ajoute des ligne ou des colonnes à ma tables même si j'applique la classe2 aprés l'ajout ou la suppression des lignes/colonnes ça ne marche pa
bn mon code le voilà
1: la classe de Model
2: la classe qui colorisla 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 import java.awt.Color; import javax.swing.table.AbstractTableModel; class MyTableModel extends AbstractTableModel { private String[] colonne ; private String Ad[][]; MyTableModel(int som,String [][]adjascence) { colonne=new String[som+1]; colonne[0]="Sommet"; Ad=new String[som][som+1]; Ad=adjascence; for(int i=1;i<=som;i++) { colonne[i]="S"+(i-1); } } public int getColumnCount() { return colonne.length; } public String [] getNonC(int a) { if(a!=0)return this.colonne; else return null; } public int getRowCount() { return Ad.length; } public String getColumnName(int col) { return colonne[col]; } public Object getValueAt(int row, int col) { return Ad[row][col]; } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public boolean isCellEditable(int row, int col) { if (col < 1) { return false; } else { return true; } } public String[] getCol(int som) { String[] as=new String[som]; return as; } }
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 java.awt.Color; import java.awt.Component; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; public class H extends DefaultTableCellRenderer { /** * */ private static final long serialVersionUID = 1L; public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { // Tu appelles la méthode par défaut, qui te construit la case Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); // Tu changes la couleur de la case if(column==0) { c.setBackground(new Color(234,234,234)); c.setForeground(Color.black); setOpaque(true); } else { c.setBackground(Color.white); c.setForeground(Color.black); setOpaque(true); } if(column==row+1) { c.setBackground(Color.gray); c.setForeground(Color.white); setOpaque(true); } return c; } }
3: j'aoutes les lignes et les colonne comme suit:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 for(int i=1;i<=som;i++) { d[i]="S"+(i-1); } d[0]="Sommet"; final DefaultTableModel modele= new DefaultTableModel(d,som); new ModelMat(adjascence,modele,som); // cette classe remplit modele avec les valeur de adjascence table.setModel(modele); table.setDefaultRenderer(String.class,new H());
à la fin sera bien remplie mais non colorie
Partager