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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
| public class JTableExemple {
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Map<String, List<Integer>> data = new LinkedHashMap<>();
data.put("Math", Arrays.asList(12,16,11));
data.put("Français", Arrays.asList(18,15));
data.put("Physique", Arrays.asList(7));
TableModel model = new AbstractTableModel() {
String[] columns = {"nom","valeurs"};
String[] matieres = data.keySet().toArray(new String[data.keySet().size()]);
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch(columnIndex) {
case 0:
return matieres[rowIndex];
case 1:
return data.get(matieres[rowIndex]);
default:
return null;
}
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
@Override
public int getRowCount() {
return matieres.length;
}
@Override
public int getColumnCount() {
return columns.length;
}
};
JTable table = new JTable(model);
table.getColumnModel().getColumn(1).setCellRenderer(new ListCellRender());
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static class ListCellRender extends JPanel implements TableCellRenderer {
private JTable table = new JTable();
private UIDefaults uidefaults = UIManager.getDefaults();
private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
private static final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
protected static Border noFocusBorder = DEFAULT_NO_FOCUS_BORDER;
private Color unselectedForeground;
private Color unselectedBackground;
public ListCellRender() {
super(new BorderLayout());
setOpaque(true);
setBorder(getNoFocusBorder());
setName("Table.cellRenderer");
add(table);
setForeground(unselectedForeground);
setBackground(unselectedBackground);
}
private Border getNoFocusBorder() {
Border border = uidefaults.getBorder("Table.cellNoFocusBorder");
if (System.getSecurityManager() != null) {
if (border != null) return border;
return SAFE_NO_FOCUS_BORDER;
} else if (border != null) {
if (noFocusBorder == null || noFocusBorder == DEFAULT_NO_FOCUS_BORDER) {
return border;
}
}
return noFocusBorder;
}
public void setForeground(Color c) {
if ( table!=null ) {
table.setForeground(c);
}
unselectedForeground = c;
}
public void setBackground(Color c) {
if ( table!=null ) {
table.setBackground(c);
}
unselectedBackground = c;
}
public void updateUI() {
super.updateUI();
setForeground(null);
setBackground(null);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Color fg = null;
Color bg = null;
JTable.DropLocation dropLocation = table.getDropLocation();
if (dropLocation != null
&& !dropLocation.isInsertRow()
&& !dropLocation.isInsertColumn()
&& dropLocation.getRow() == row
&& dropLocation.getColumn() == column) {
fg = uidefaults.getColor("Table.dropCellForeground");
bg = uidefaults.getColor("Table.dropCellBackground");
isSelected = true;
}
if (isSelected) {
super.setForeground(fg == null ? table.getSelectionForeground()
: fg);
super.setBackground(bg == null ? table.getSelectionBackground()
: bg);
} else {
Color background = unselectedBackground != null
? unselectedBackground
: table.getBackground();
if (background == null || background instanceof javax.swing.plaf.UIResource) {
Color alternateColor = uidefaults.getColor("Table.alternateRowColor");
if (alternateColor != null && row % 2 != 0) {
background = alternateColor;
}
}
super.setForeground(unselectedForeground != null
? unselectedForeground
: table.getForeground());
super.setBackground(background);
}
setFont(table.getFont());
if (hasFocus) {
Border border = null;
if (isSelected) {
border = uidefaults.getBorder("Table.focusSelectedCellHighlightBorder");
}
if (border == null) {
border = uidefaults.getBorder("Table.focusCellHighlightBorder");
}
setBorder(border);
if (!isSelected && table.isCellEditable(row, column)) {
Color col;
col = uidefaults.getColor("Table.focusCellForeground");
if (col != null) {
super.setForeground(col);
}
col = uidefaults.getColor("Table.focusCellBackground");
if (col != null) {
super.setBackground(col);
}
}
} else {
setBorder(getNoFocusBorder());
}
List<Integer> list = (List<Integer>)value;
Object[][] data = new Object[list.size()][1];
for(int i=0; i<list.size(); i++) {
data[i][0]=list.get(i);
}
this.table.setModel(new DefaultTableModel(data, new String[1]));
int height=(int)this.table.getPreferredSize().getHeight();
table.setRowHeight(row,height);
return this;
}
}
} |
Partager