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
|
public class FTComboEditor extends AbstractCellEditor
implements TableCellEditor, ActionListener
{
JComboBox myCombo;
String myValue;
String[] notes;
public FTComboEditor()
{
notes = new String[]{"", "1", "2", "3"};
myCombo = new JComboBox(notes);
myCombo.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
JComboBox cb = (JComboBox)e.getSource();
String newSelection = (String)cb.getSelectedItem();
// Si on choisit une note, on la supprime de la liste:
if (!newSelection.isEmpty())
{
myValue = newSelection;
int id = cb.getSelectedIndex();
myCombo.removeItemAt(id);
}
else
//Si on choist "" sur une cellule non vide,
//on ré-insère la valeur dans la Combo et on met la cellule à vide:
{
if(!myValue.isEmpty())
{
myCombo.addItem(myValue);
myValue = newSelection;
}
}
fireEditingStopped();
}
public Object getCellEditorValue()
{
return myValue;
}
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column)
{
myValue = (String)value;
return myCombo;
}
} |
Partager