@Override public Component getListCellRendererComponent( JList<? extends E> list, E value, int index, boolean isSelected, boolean cellHasFocus) { label.setText(Objects.toString(value, "")); this.list = list; this.index = index; if (isSelected) { setBackground(list.getSelectionBackground()); label.setForeground(list.getSelectionForeground()); } else { setBackground(index % 2 == 0 ? EVEN_COLOR : list.getBackground()); label.setForeground(list.getForeground()); } MutableComboBoxModel m = (MutableComboBoxModel) list.getModel(); if (index < 0 || m.getSize() - 1 <= 0) { setOpaque(false); deleteButton.setVisible(false); label.setForeground(list.getForeground()); } else { boolean f = index == rolloverIndex; setOpaque(true); deleteButton.setVisible(true); deleteButton.getModel().setRollover(f); deleteButton.setForeground(f ? Color.WHITE : list.getForeground()); } return this; }
@Override public void actionPerformed(ActionEvent e) { MutableComboBoxModel m = (MutableComboBoxModel) list.getModel(); if (m.getSize() > 1) { m.removeElementAt(index); comboBox.showPopup(); } }
/** 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; } } } }
/** * 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); }