Java Code Examples for javax.swing.DefaultComboBoxModel#insertElementAt()

The following examples show how to use javax.swing.DefaultComboBoxModel#insertElementAt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LicenseHeadersPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean selectComboBoxItem(String name, boolean setText) {
    boolean found = false;
    DefaultComboBoxModel model = (DefaultComboBoxModel) comGlobal.getModel();
    for (int i = 0; i < model.getSize(); i++) {
        GlobalItem gi = (GlobalItem) model.getElementAt(i);
        if (gi.getName().equals(name)) {
            comGlobal.setSelectedItem(gi);
            found = true;
            if (setText) {
                setTextToGlobalLicense();
            }
            break;
        }
    }
    if (!found) {
        GlobalItem itm = new GlobalItem(name, null);
        model.insertElementAt(itm, 0);
        comGlobal.setSelectedItem(itm);
        if (setText) {
            setTextToGlobalLicense();
        }
    }
    return found;
}
 
Example 2
Source File: DriverSettings.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
private void renameEmulator() {
    String oldName = browserCombo.getSelectedItem().toString();
    String newEmName = browserCombo.getEditor().getItem().toString();
    if (!oldName.equals(newEmName)) {
        if (!getTotalBrowserList().contains(newEmName)) {
            Emulator emulator = settings.getEmulators().getEmulator(oldName);
            emulator.setName(newEmName);
            DefaultComboBoxModel combomodel = (DefaultComboBoxModel) browserCombo.getModel();
            DefaultComboBoxModel dupCombomodel = (DefaultComboBoxModel) dupDriverCombo.getModel();
            int index = browserCombo.getSelectedIndex();
            combomodel.removeElement(oldName);
            dupCombomodel.removeElement(oldName);
            combomodel.insertElementAt(newEmName, index);
            dupCombomodel.insertElementAt(newEmName, index);
            browserCombo.setSelectedIndex(index);
        } else {
            Notification.show("Emulator/Browser [" + newEmName + "] already Present");
        }
    }
}
 
Example 3
Source File: GUIUtils.java    From open-ig with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
   * Checks if the item is in the combobox. If yes, then it is moved to the beginning,
   * otherwise, the item is added as first.
   * @param <T> the element type
   * @param combobox the combobox
   * @param item the item
   */
  public static <T> void addFirstItem(JComboBox<T> combobox, T item) {
  	int idx = -1;
  	DefaultComboBoxModel<T> model = (DefaultComboBoxModel<T>)combobox.getModel();
for (int i = 0; i < model.getSize(); i++) {
  		T t = model.getElementAt(i);
  		if (Objects.equals(t, item)) {
  			idx = i;
  			break;
  		}
  	}
model.insertElementAt(item, 0);
  	if (idx >= 0) {
  		model.removeElementAt(idx + 1);
  	}
  }
 
Example 4
Source File: ColorComboBox.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Creates a new color combobox and populates it with the excel colors.
 */
public ColorComboBox() {
  final DefaultComboBoxModel model = new DefaultComboBoxModel( ColorUtility.getPredefinedExcelColors() );
  model.insertElementAt( null, 0 );
  model.setSelectedItem( null );

  setModel( model );
  setRenderer( new ColorCellRenderer() );
  final int height1 = getPreferredSize().height;
  setMaximumSize( new Dimension( height1 * 4, height1 ) );
  setFocusable( false );

  final boolean isGTK = isGTKLookAndFeel();
  setEditable( isGTK );

  // if it's GTK LnF, we have to customize the combo box since GTK Lnf ignores the <i>setBackground</i>
  if ( isGTK ) {
    setEditor( new CustomGTKComboBoxEditor() );
  }
}
 
Example 5
Source File: SummaryTablePanel.java    From nmonvisualizer with Apache License 2.0 6 votes vote down vote up
private void updateStatisticsComboBox() {
    String newName = Statistic.GRANULARITY_MAXIMUM.getName(gui.getGranularity());

    @SuppressWarnings("unchecked")
    DefaultComboBoxModel<Object> model = (DefaultComboBoxModel<Object>) ((JComboBox<Object>) statsPanel
            .getComponent(1)).getModel();

    boolean reselect = false;

    if (model.getSelectedItem() == model.getElementAt(4)) {
        reselect = true;
    }

    model.removeElementAt(4);
    model.insertElementAt(newName, 4);

    if (reselect) {
        model.setSelectedItem(newName);
    }
}
 
Example 6
Source File: DirectorySelectorCombo.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private ComboListElement addPath(String path) {
  DefaultComboBoxModel model = (DefaultComboBoxModel)fileMRU.getModel();
  ComboListElement newPath = new StringComboListElement(path);
  int index = model.getIndexOf(newPath);
  if (index == -1) {
    model.insertElementAt(newPath, 1);
  }
  if (model.getSize() > itemCountLimit + 3) {
    model.removeElementAt(model.getSize() - 3);
  }
  return newPath;
}
 
Example 7
Source File: RunAsRemoteWeb.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void selectRemoteConnection(String remoteConnection) {
    if (remoteConnection == null) {
        remoteConnection = getValue(PhpProjectProperties.REMOTE_CONNECTION);
    }
    // #141849 - can be null if one adds remote config for the first time for a project but already has some remote connection
    DefaultComboBoxModel<RemoteConfiguration> model = (DefaultComboBoxModel<RemoteConfiguration>) remoteConnectionComboBox.getModel();
    if (remoteConnection == null
            || RunConfigRemote.NO_CONFIG_NAME.equals(remoteConnection)) {
        if (model.getIndexOf(RunConfigRemote.NO_REMOTE_CONFIGURATION) < 0) {
            model.insertElementAt(RunConfigRemote.NO_REMOTE_CONFIGURATION, 0);
        }
        remoteConnectionComboBox.setSelectedItem(RunConfigRemote.NO_REMOTE_CONFIGURATION);
        return;
    }

    int size = remoteConnectionComboBox.getModel().getSize();
    for (int i = 0; i < size; ++i) {
        RemoteConfiguration rc = remoteConnectionComboBox.getItemAt(i);
        if (remoteConnection.equals(rc.getName())) {
            remoteConnectionComboBox.setSelectedItem(rc);
            return;
        }
    }

    // remote connection is missing (probably removed?)
    remoteConnectionComboBox.addItem(RunConfigRemote.MISSING_REMOTE_CONFIGURATION);
    remoteConnectionComboBox.setSelectedItem(RunConfigRemote.MISSING_REMOTE_CONFIGURATION);
    // # 162230
    model.removeElement(RunConfigRemote.NO_REMOTE_CONFIGURATION);
}
 
Example 8
Source File: TemplateChooserPanelGUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void selectProject (@NullAllowed Project p) {
    if (p != null) {
        DefaultComboBoxModel projectsModel = (DefaultComboBoxModel) projectsComboBox.getModel ();
        if ( projectsModel.getIndexOf( p ) == -1 ) {
            projectsModel.insertElementAt( p, 0 );
        }
        projectsComboBox.setSelectedItem( p );
    } 
    else {
        projectsComboBox.setSelectedItem(null);
    }
}
 
Example 9
Source File: LuceneDataStoreSearchGUI.java    From gate-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void updateAnnotationSetsList() {
  String corpusName =
          (corpusToSearchIn.getSelectedItem()
                  .equals(Constants.ENTIRE_DATASTORE))
                  ? null
                  : (String)corpusIds
                          .get(corpusToSearchIn.getSelectedIndex() - 1);
  TreeSet<String> ts = new TreeSet<String>(stringCollator);
  ts.addAll(getAnnotationSetNames(corpusName));
  DefaultComboBoxModel<String> dcbm = new DefaultComboBoxModel<String>(ts.toArray(new String[ts.size()]));
  dcbm.insertElementAt(Constants.ALL_SETS, 0);
  annotationSetsToSearchIn.setModel(dcbm);
  annotationSetsToSearchIn.setSelectedItem(Constants.ALL_SETS);

  // used in the ConfigureStackViewFrame as Annotation type column
  // cell editor
  TreeSet<String> types = new TreeSet<String>(stringCollator);
  types.addAll(getTypesAndFeatures(null, null).keySet());
  // put all annotation types from the datastore
  // combobox used as cell editor
  JComboBox<String> annotTypesBox = new JComboBox<String>();
  annotTypesBox.setMaximumRowCount(10);
  annotTypesBox.setModel(new DefaultComboBoxModel<String>(types.toArray(new String[types.size()])));
  DefaultCellEditor cellEditor = new DefaultCellEditor(annotTypesBox);
  cellEditor.setClickCountToStart(0);
  configureStackViewFrame.getTable().getColumnModel()
          .getColumn(ANNOTATION_TYPE).setCellEditor(cellEditor);
}
 
Example 10
Source File: TMSettings.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
private void renameModule() {
    if (moduleCombo.getSelectedIndex() != -1) {
        String moduleName = moduleCombo.getSelectedItem().toString();
        String newModuleName = moduleCombo.getEditor().getItem().toString();
        if (!newModuleName.trim().isEmpty()) {
            testMgmtModule.getModule(moduleName).setModule(newModuleName);
            DefaultComboBoxModel combomodel = (DefaultComboBoxModel) moduleCombo.getModel();
            int index = moduleCombo.getSelectedIndex();
            combomodel.removeElement(moduleName);
            combomodel.insertElementAt(newModuleName, index);
            moduleCombo.setSelectedIndex(index);
        }
    }
}
 
Example 11
Source File: DemoUtils.java    From java-ocr-api with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void savePrefs(Preferences prefs, String prefKey, JComboBox combo, String newValidValue) {
    if (newValidValue == null) { 
        return;
    }

    DefaultComboBoxModel comboModel = (DefaultComboBoxModel) combo.getModel();

    int existingIndex = comboModel.getIndexOf(newValidValue);
    if (existingIndex >= 0) { 
        comboModel.removeElementAt(existingIndex);
    }
    comboModel.insertElementAt(newValidValue, 0);
    combo.setSelectedIndex(0);

    StringBuilder entries = new StringBuilder();
    int size = Math.min(comboModel.getSize(), 20); 
    for (int i = 0; i < size; i++) {
        entries.append(comboModel.getElementAt(i));
        if (i != size - 1) { 
            entries.append(DELIMITER);
        }
    }

    while (entries.length() > Preferences.MAX_VALUE_LENGTH) {
        int lastIndex = entries.lastIndexOf(DELIMITER);
        if (lastIndex == -1) {
            break;
        } else {
            entries.delete(lastIndex, entries.length());
        }
    }

    prefs.put(prefKey, entries.toString());
    try {
        prefs.flush();
    } catch (Throwable e) {
        e.printStackTrace();
    }
}