Bonjour à tous. Je sais que ce sujet à déjà été traité des miliers de fois mais je n'ai toujours pas acquis le principe ce que je veux faire c'est créer un jtable avec une colonne combobox et selon les valeurs de ma combobox une couleur est attribué à la ligne de l'élément sélectionné voilà mon code
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
package tp;
import javax.swing.table.AbstractTableModel;
 
public class MonModele extends AbstractTableModel{
	private static final long serialVersionUID = 1L;
	private String[] columnNames={"Matricule","Noms et Prénoms","Moyènne","Absences","Décisions"};
	private String[][] data=null;
	public MonModele(String[][] data){
		this.data=data;
	}
 
	public int getColumnCount() {
        return columnNames.length;
    }
 
    public int getRowCount() {
        return data.length;
    }
 
    public String getColumnName(int col) {
        return columnNames[col];
    }
 
    public Object getValueAt(int row, int col) {
        return data[row][col];
    }
 
    public boolean isCellEditable(int row, int col) {
            return false;
    }
}
Pour mon modèle
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
package tp;
 
import java.awt.Color;
import java.awt.Component;
 
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
 
public class ColorRenderer extends DefaultTableCellRenderer {
	private static final long serialVersionUID = 1L;
 
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		Component cell = super.getTableCellRendererComponent(table, value,
				isSelected, hasFocus, row, column);				
			if(table.getValueAt(row,column).toString().compareTo("Admis en classe supèrieur") == 0){
				cell.setBackground(Color.BLUE);
				cell.setForeground(Color.GRAY);
			}
			else if(table.getValueAt(row,column).toString().compareTo("Exclu pour conduite") == 0){
				cell.setBackground(Color.RED);
				cell.setForeground(Color.CYAN);
			}
			else{
				cell.setBackground(Color.DARK_GRAY);
				cell.setForeground(Color.WHITE);
 
			}		
		return cell;
	}
}
Pour mon rendu
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
75
76
package tp;
import java.awt.Dimension;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
 
public class TestModeleTable extends JPanel{
	private static final long serialVersionUID = 1L;	
 
	public TestModeleTable() {       
		String[][] data={{"CM04-09IUT0283","kjsdbckdzala","14.23","20","Admis en classe supèrieur"},{"CM04609IUT0013","ljkzbachazvbc","14.23","10","Exclu pour conduite"},{"CM04609IUT0025","TOTO TATA Titi","09","50","Admis en classe supèrieur"}};
        JTable table = new JTable(new MonModele(data));
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
 
        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);        
 
 
        //Fiddle with the Sport column's cell editors/renderers.        
        	setUpSportColumn(table, table.getColumnModel().getColumn(4));               
 
        //Add the scroll pane to this panel.
        add(scrollPane);
	}
 
 
    public void setUpSportColumn(JTable table,
            TableColumn DecisionColumn) {
		//Set up the editor for the sport cells.
		JComboBox comboBox = new JComboBox();
		comboBox.addItem("Admis en classe supèrieur");
		comboBox.addItem("Admis au rattrappage");
		comboBox.addItem("Admis à redoubler");		
		comboBox.addItem("Redouble en cas d'echec");
		comboBox.addItem("Exclu en cas d'echec");
		comboBox.addItem("Exclu pour conduite");
		DecisionColumn.setCellEditor(new DefaultCellEditor(comboBox));
 
		//	Set up tool tips for the sport cells.
		ColorRenderer renderer =
		new ColorRenderer();
		renderer.setToolTipText("Click for combo box");
		DecisionColumn.setCellRenderer(renderer);		
    }
 
 
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TableRenderDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Create and set up the content pane.
        TestModeleTable newContentPane = new TestModeleTable();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);
 
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
 
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
Pour le test mais sa ne marche pas