/** * Insert the given item into the combo box, and set it as first selected item. If the item * already exists, it is removed, so there are no duplicates. * * @param item the item to insert. if it's null, then nothing is inserted */ public static void insertIntoCombo(JComboBox combo, Object item) { if (item == null) { return; } MutableComboBoxModel model = (MutableComboBoxModel) combo.getModel(); if (model.getSize() == 0) { model.insertElementAt(item, 0); return; } Object o = model.getElementAt(0); if (o.equals(item)) { return; } model.removeElement(item); model.insertElementAt(item, 0); combo.setSelectedIndex(0); }
/** This method is used to change the dynamically label of an object in this JInvidChooser. */ public void relabelObject(Invid invid, String newLabel) { MutableComboBoxModel model = (MutableComboBoxModel) getModel(); synchronized (model) { for (int i = 0; i < model.getSize(); i++) { listHandle lh = (listHandle) model.getElementAt(i); if (lh != null && lh.getObject() != null && lh.getObject().equals(invid)) { model.removeElementAt(i); lh.setLabel(newLabel); model.insertElementAt(lh, i); repaint(); break; } } } }