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
| public class TestJTable extends JFrame {
JComboBox comboBoxGenderEditor = new JComboBox(
new String[] {"M.", "Mme", "Mlle"});
JComboBox comboBoxEnglishGenderEditor = new JComboBox(
new String[] {"Mr", "Mme", "Miss"});
JComboBox comboBoxAgeEditor = new JComboBox(new Vector() {
{
for (int i = 0; i < 120; i += 10) {
add(age(i));
}
}
});
public TestJTable() {
JTable table = new SpecialTable(
new Object[][] {
new Object[] {"M.", "Mme", "Miss"},
new Object[] {"Dupont", "Durant", "Smith"},
new Object[] {age(11), age(43), age(60)}
},
new Object[] {"A", "B", "C"});
JScrollPane sp = new JScrollPane(table);
getContentPane().add(sp);
setSize(250, 110);
}
private String age(int age) {
int temp = (age / 10) * 10;
return temp + "-" + (temp + 9);
}
class SpecialTable extends JTable {
public SpecialTable(Object[][] rowData, Object[] columnNames) {
super(rowData, columnNames);
}
public TableCellEditor getCellEditor(int row, int column) {
return (row == 0
? (column == 2
? new DefaultCellEditor(comboBoxEnglishGenderEditor)
: new DefaultCellEditor(comboBoxGenderEditor))
: (row == 2
? new DefaultCellEditor(comboBoxAgeEditor)
: super.getCellEditor(row, column)));
}
public static void main(String[] args) {
new TestJTable().setVisible(true);
}
} |
Partager