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

The following examples show how to use javax.swing.DefaultComboBoxModel#getElementAt() . 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: SelectProjectPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void btnProjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProjectActionPerformed
    JFileChooser chooser = ProjectChooser.projectChooser();
    int res = chooser.showOpenDialog(SwingUtilities.getWindowAncestor(this));
    if (res == JFileChooser.APPROVE_OPTION) {
        File fil = chooser.getSelectedFile();
        FileObject fo = FileUtil.toFileObject(fil);
        if (fo != null) {
            try {
                Project p = ProjectManager.getDefault().findProject(fo);
                DefaultComboBoxModel model = (DefaultComboBoxModel)comProject.getModel();
                model.addElement(p);
                model.setSelectedItem(p);
                if (EMPTY == model.getElementAt(0)) {
                    model.removeElement(EMPTY);
                }
            } catch (IOException exc) {
                ErrorManager.getDefault().notify(exc);
            }
        }
    }
}
 
Example 2
Source File: MultiDiffPanelController.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addToModel (final Revision newItem, final JComboBox cmbDiffTree) {
    DefaultComboBoxModel model = (DefaultComboBoxModel) cmbDiffTree.getModel();
    for (int i = 0; i < model.getSize(); ++i) {
        final Object item = model.getElementAt(i);
        if (item instanceof Revision && ((Revision) item).getCommitId().equals(newItem.getCommitId())
                && ((Revision) item).getRevision().equals(newItem.getRevision())) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run () {
                    cmbDiffTree.setSelectedItem(item);
                }
            });
            return;
        }
    }
    model.addElement(newItem);
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run () {
            cmbDiffTree.setSelectedItem(newItem);
        }
    });
}
 
Example 3
Source File: Repository.java    From netbeans with Apache License 2.0 6 votes vote down vote up
RepositoryConnection getSelectedRCIntern() {
    String urlString;
    try {
        urlString = getUrlString();            
    }
    catch (InterruptedException ex) {
        // should not happen
        Subversion.LOG.log(Level.SEVERE, null, ex);
        return null;
    }
    
    DefaultComboBoxModel dcbm = (DefaultComboBoxModel) repositoryPanel.urlComboBox.getModel();                
    int idx = dcbm.getIndexOf(urlString);        
    
    if(idx > -1) {
        return (RepositoryConnection) dcbm.getElementAt(idx);
    }        
    return getEditedRC();        
}
 
Example 4
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 5
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 6
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 7
Source File: ModernComboCheckBox.java    From nanoleaf-desktop with MIT License 5 votes vote down vote up
private String[] modelToArray(DefaultComboBoxModel<T> model)
{
	String[] items = new String[model.getSize()];
	for (int i = 0; i < model.getSize(); i++)
	{
		items[i] = (String)model.getElementAt(i);
	}
	return items;
}
 
Example 8
Source File: PersistenceProviderComboboxHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void initCombo(JComboBox providerCombo) {
    
    DefaultComboBoxModel providers = new DefaultComboBoxModel();
    
    for(Provider each : providerSupplier.getSupportedProviders()){
       providers.addElement(each);
    }

    if (providers.getSize() == 0 && providerSupplier.supportsDefaultProvider()){
        providers.addElement(ProviderUtil.DEFAULT_PROVIDER);
    } 

    if (providers.getSize() == 0){
        providers.addElement(EMPTY);
    }
    
    providerCombo.setModel(providers);
    providerCombo.addItem(SEPARATOR);
    providerCombo.addItem(new NewPersistenceLibraryItem());
    providerCombo.addItem(new ManageLibrariesItem());
    providerCombo.setRenderer(new PersistenceProviderCellRenderer(getDefaultProvider(providers)));
    //select either default or first or preferred provider depending on project details
    int selectIndex = 0;
    if(providers.getSize()>1 && providers.getElementAt(0) instanceof Provider){
        String defProviderVersion = ProviderUtil.getVersion((Provider) providers.getElementAt(0));
        boolean specialCase = (Util.isJPAVersionSupported(project, Persistence.VERSION_2_0) || Util.isJPAVersionSupported(project, Persistence.VERSION_2_1)) && (defProviderVersion == null || defProviderVersion.equals(Persistence.VERSION_1_0));//jpa 2.0 is supported by default (or first) is jpa1.0 or udefined version provider
        if(specialCase){
            for (int i = 1; i<providers.getSize() ; i++){
                if(preferredProvider.equals(providers.getElementAt(i))){
                    selectIndex = i;
                    break;
                }
            }
        }
    }
    providerCombo.setSelectedIndex(selectIndex);
}
 
Example 9
Source File: ClassNamePanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates new form ClassNamePanel
 */
public ClassNamePanel(Project project, FileObject anchor, String initialName) {
    this.project = project;
    this.anchor = anchor;
    initComponents();
    
    Project openProjects[] = OpenProjects.getDefault().getOpenProjects();
    Arrays.sort( openProjects, new ProjectByDisplayNameComparator());
    DefaultComboBoxModel projectsModel = new DefaultComboBoxModel( openProjects );
    projectSelector.setModel( projectsModel );                
    if (project != null) {
        projectSelector.setSelectedItem( project );
        projectSelector.setEnabled(false);
        projectLabel.setEnabled(false);
    } else if (projectsModel.getSize() > 0) {
        this.project = (Project)projectsModel.getElementAt(0);
        projectsModel.setSelectedItem(this.project);
    }
    projectSelector.setRenderer(new ProjectCellRenderer());
    locationSelect.setRenderer(new GroupCellRenderer());
    packageSelect.setRenderer(PackageView.listRenderer());
    
    updateRoots();
    updatePackages();
    
    selectInitialPackage();
    
    ActionListener al = this::actionPerformed;
    locationSelect.addActionListener(al);
    packageSelect.addActionListener(al);
    packageSelect.getEditor().addActionListener(al);
    projectSelector.addActionListener(al);
    className.getDocument().addDocumentListener(this);
    
    if (initialName != null) {
        className.setText(initialName);
    }
}
 
Example 10
Source File: DirectorySelectorCombo.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void fileMRUPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_fileMRUPopupMenuWillBecomeVisible
  DefaultComboBoxModel model = (DefaultComboBoxModel)fileMRU.getModel();
  Collection mukls = new ArrayList();
  for(int i=0;i<model.getSize();i++) {
    if (!(model.getElementAt(i) instanceof ComboListElement))
      continue;
    if (((ComboListElement)model.getElementAt(i)).isVolatile())
      mukls.add(model.getElementAt(i));
  }
  for (Iterator it = mukls.iterator(); it.hasNext();) {
    Object elem = (Object) it.next();
    model.removeElement(elem);
    it.remove();
  }
}
 
Example 11
Source File: TargetPanel.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Load all targeted factory configuration instances and update user interface
 * to show them.
 *
 * @param selectedPid
 *          If this PID is available then select it, otherwise select the last
 *          PID. If {@link #nextFactoryPidToSelect} is non-null then select
 *          that configuration instance and set the field to {@code null}.
 */
private void updateSelectionFactoryPID(String selectedPid)
{
  synchronized (pid2Cfg) {
    pid2Cfg.clear();
    if (selectedPid == null) {
      selectedPid = "";
    }

    for (int i = 0; i < MAX_SIZE; i++) {
      try {
        final Configuration[] configs =
          CMDisplayer.getCA().listConfigurations("(service.factoryPid="
                                                     + targetedPids[i] + ")");
        if (configs != null) {
          for (final Configuration cfg : configs) {
            pid2Cfg.put(cfg.getPid(), cfg);
          }
        }
      } catch (final Exception e) {
        Activator.log
            .error("Faile to load factory configuration instances for fpid '"
                   + targetedPids[i] + "': " + e.getMessage(), e);
      }
    }

    final SortedSet<String> instancePIDs =
      new TreeSet<String>(pid2Cfg.keySet());
    instancePIDs.add(FACTORY_PID_DEFAULTS);

    final DefaultComboBoxModel model =
      new DefaultComboBoxModel(instancePIDs.toArray());
    if (nextFactoryPidToSelect != null) {
      if (instancePIDs.contains(nextFactoryPidToSelect)) {
        selectedPid = nextFactoryPidToSelect;
      }
      nextFactoryPidToSelect = null;
    } else if (!instancePIDs.contains(selectedPid)) {
      // New selection needed, use last PID.
      selectedPid = (String) model.getElementAt(model.getSize() - 1);
    }
    model.setSelectedItem(selectedPid);
    fbox.setModel(model);
    final Configuration selectedCfg = pid2Cfg.get(selectedPid);

    // Update the targeted PID selectors to match the target selectors in the
    // factory PID of the selected instance.
    final String fpid =
      selectedCfg != null ? selectedCfg.getFactoryPid() : targetedPids[0];
    String tpid = null;
    for (int i = 0; i < MAX_SIZE && null != (tpid = targetedPids[i]); i++) {
      rbs[i].setToolTipText(TARGET_LEVEL_FACOTRY_PID_TOOLTIPS[i] + tpid
                            + "</code></p></html>");

      if (fpid.equals(targetedPids[i])) {
        rbs[i].setSelected(true);
        selectedTargetLevel = i;
        if (selectedCfg != null) {
          icons[i].setIcon(openDocumentIcon);
          icons[i].setToolTipText("exists");
        } else {
          icons[i].setIcon(newDocumentIcon);
          icons[i].setToolTipText("to be created");
        }
      } else {
        icons[i].setIcon(newDocumentIcon);
        icons[i].setToolTipText("to be created");
      }
    }
  }
}