Bonjour,
J'avoue avoir un peu de mal avec les tables, et même avec Java tout court mais je persiste !
Je tente de faire un tri sur la colonne d'une table qui est censé contenir des int (que je transforme en double), mais quand je la trie sur l'entête, il me la trie comme des chaines de caractères.
Voici mon code :
mon model :
Mon renderer :
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 import java.text.DecimalFormat; import javax.swing.table.AbstractTableModel; public class HandsTableModel extends AbstractTableModel { private static final long serialVersionUID = 3685438063323573991L; private String[] columnNames = {"Game", "Hand", "Board", "Gain"}; private Object[][] data = {}; boolean[] canEdit = new boolean [] { false, false, false, false }; 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) { if(col==3){ double i = ((Integer)data[row][col]).doubleValue(); double d = i/100; DecimalFormat myFormatter = new DecimalFormat("0.00"); String output = myFormatter.format(d); return output.replaceAll(",","."); }else{ return data[row][col]; } } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } /* * Don't need to implement this method unless your table's * editable. */ public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit [columnIndex]; } /* * Don't need to implement this method unless your table's * data can change. */ public void setValueAt(Object value, int row, int col) { data[row][col] = value; fireTableCellUpdated(row, col); } public void setData(Object[][] tablo) { if(tablo==null){ data = new Object[0][0]; }else{ data = tablo; fireTableDataChanged(); } } }
et le code de la 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 import java.awt.Component; import java.awt.Color; import javax.swing.JTable; import javax.swing.SwingConstants; import javax.swing.table.DefaultTableCellRenderer; public class HandsTableRenderer extends DefaultTableCellRenderer { private static final long serialVersionUID = 2159585159048146840L; 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); // Colonne Gain if(column==3){ String val = ((String)value); double dval = Double.parseDouble(val); if(dval > 0){ cell.setForeground( Color.blue ); }else{ if(dval < 0){ cell.setForeground( Color.red ); }else{ cell.setForeground( Color.black ); } } setHorizontalAlignment(SwingConstants.RIGHT); } return cell; } }
Je ne vois pas comment trier sur la "valeur" et non pas sur la "forme"...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 model = new HandsTableModel(); jTable1 = new JTable(model); TableCellRenderer renderer = new HandsTableRenderer(); jTable1.getColumnModel().getColumn(3).setCellRenderer(renderer); jTable1.setAutoCreateRowSorter(true);
Merci à vous
Partager