@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;
 }
 /** Generate a ComboBoxModel to be used by the Instrument Selection combo box */
 private javax.swing.ComboBoxModel generateInstrumentSelectModel() {
   javax.swing.MutableComboBoxModel InstrumentSelectModel = new javax.swing.DefaultComboBoxModel();
   // Iterate through the instruments in the orchestra, and add them to the combo box.
   for (int i = 0; i < 128; i++) {
     InstrumentSelectModel.addElement(myOrchestra.getInstrumentName(i));
   }
   return (javax.swing.ComboBoxModel) InstrumentSelectModel;
 }
 @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;
        }
      }
    }
  }
Exemple #5
0
  /**
   * 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);
  }