Java Code Examples for javax.swing.JComboBox#getModel()

The following examples show how to use javax.swing.JComboBox#getModel() . 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: PmdDataSourceEditor.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void setScriptingLanguage( final String lang, final JComboBox languageField ) {
  if ( lang == null ) {
    languageField.setSelectedItem( null );
    return;
  }

  final ListModel model = languageField.getModel();
  for ( int i = 0; i < model.getSize(); i++ ) {
    final ScriptEngineFactory elementAt = (ScriptEngineFactory) model.getElementAt( i );
    if ( elementAt == null ) {
      continue;
    }
    if ( elementAt.getNames().contains( lang ) ) {
      languageField.setSelectedItem( elementAt );
      return;
    }
  }
}
 
Example 2
Source File: QueryEditorPanel.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void setScriptingLanguage( final String lang, final JComboBox languageField ) {
  if ( lang == null ) {
    languageField.setSelectedItem( null );
    return;
  }

  final ListModel model = languageField.getModel();
  for ( int i = 0; i < model.getSize(); i++ ) {
    final ScriptEngineFactory elementAt = (ScriptEngineFactory) model.getElementAt( i );
    if ( elementAt == null ) {
      continue;
    }
    if ( elementAt.getNames().contains( lang ) ) {
      languageField.setSelectedItem( elementAt );
      return;
    }
  }
}
 
Example 3
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 4
Source File: ActionUtils.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 combo
 * @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);
}
 
Example 5
Source File: JdbcDataSourceDialog.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void setScriptingLanguage( final String lang, final JComboBox languageField ) {
  if ( lang == null ) {
    languageField.setSelectedItem( null );
    return;
  }

  final ListModel model = languageField.getModel();
  for ( int i = 0; i < model.getSize(); i++ ) {
    final ScriptEngineFactory elementAt = (ScriptEngineFactory) model.getElementAt( i );
    if ( elementAt == null ) {
      continue;
    }
    if ( elementAt.getNames().contains( lang ) ) {
      languageField.setSelectedItem( elementAt );
      return;
    }
  }
}
 
Example 6
Source File: ActionUtils.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 combo
 * @param item
 */
public static void insertIntoCombo(JComboBox combo, Object item) {
    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);
}
 
Example 7
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 8
Source File: AbstractSwingGuiCallback.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void updateComboBoxes(List<FormItem> items) {
    for (FormItem item : items) {
        JComponent field = item.getField();
        if (field instanceof JComboBox) {
            @SuppressWarnings("unchecked")
            JComboBox<String> box = (JComboBox<String>) field;
            List<String> newPossibleValues = item.getPossibleValues();
            if (!boxModelIsSame(box, newPossibleValues)) {
                MutableComboBoxModel<String> mmodel = (MutableComboBoxModel<String>) box.getModel();
                replaceBoxModelValues(mmodel, newPossibleValues);
                mmodel.setSelectedItem(item.getCurrentValue());
            }
        }
    }
}
 
Example 9
Source File: DropDownParameterComponent.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void actionPerformed( final ActionEvent e ) {
  if ( adjustingToExternalInput ) {
    return;
  }

  final Object theSource = e.getSource();
  if ( theSource instanceof JComboBox ) {
    final JComboBox theComboBox = (JComboBox) theSource;
    final KeyedComboBoxModel theModel = (KeyedComboBoxModel) theComboBox.getModel();
    updateContext.setParameterValue( key, theModel.getSelectedKey() );
  }
}
 
Example 10
Source File: UpdateSummaryTable.java    From bigtable-sql with Apache License 2.0 5 votes vote down vote up
private void setModel(JComboBox box, ArtifactAction... actions) {
	ComboBoxModel oldModel = box.getModel();
	box.setModel(getComboBoxModel(actions));
	if (oldModel.getSize() != actions.length) {
		box.firePropertyChange("itemCount", oldModel.getSize(), actions.length);
	}
}
 
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();
    }
}
 
Example 12
Source File: PanelOcrInput.java    From java-ocr-api with GNU Affero General Public License v3.0 5 votes vote down vote up
static void selectCombo(JComboBox comboBox, String value) {
    if(value == null || value.trim().length() == 0) {
        return;
    }
    ComboBoxModel model = comboBox.getModel();
    for(int i = 0; i < model.getSize(); i++) {
        if(value.equals(model.getElementAt(i).toString())) {
            comboBox.setSelectedIndex(i);
            return;
        }
    }
}
 
Example 13
Source File: GenerateCodeDialog.java    From jeddict with Apache License 2.0 5 votes vote down vote up
private void setPackage(JComboBox packageCombo, String _package) {
    ComboBoxModel model = packageCombo.getModel();
    for (int i = 0; i < model.getSize(); i++) {
        if (model.getElementAt(i).toString().equals(_package)) {
            model.setSelectedItem(model.getElementAt(i));
            return;
        }
    }
    ((JTextComponent) packageCombo.getEditor().getEditorComponent()).setText(_package);
}
 
Example 14
Source File: CopyDialogTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void assertModel (Map<String, String> items, JComboBox combo, List<String> expected) {
    ComboBoxModel model = combo.getModel();
    assertEquals(expected.size() / 2, model.getSize());
    for (int i = 0; i < model.getSize(); ++i) {
        assertEquals(expected.get(i * 2), model.getElementAt(i));
        assertEquals(expected.get(i * 2 + 1), items.get((String) model.getElementAt(i)));
    }
}
 
Example 15
Source File: ReturnTypeUIHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void performBrowseType(final JComboBox combo, final ClasspathInfo cpInfo) {
    final ReturnTypeComboBoxModel model = (ReturnTypeComboBoxModel) combo.getModel();
    combo.setPopupVisible(false);
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            final ElementHandle<TypeElement> handle = TypeElementFinder.find(cpInfo, new TypeElementFinder.Customizer() {
                public Set<ElementHandle<TypeElement>> query(ClasspathInfo classpathInfo, String textForQuery, NameKind nameKind, Set<SearchScope> searchScopes) {//GEN-LAST:event_browseButtonActionPerformed
                    return classpathInfo.getClassIndex().getDeclaredTypes(textForQuery, nameKind, searchScopes);
                }

                public boolean accept(ElementHandle<TypeElement> typeHandle) {
                    return true;
                }
            });

            combo.setPopupVisible(false);
            
            if (handle == null) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        setSelectedItem(combo, model.getPreviousItem());
                    }
                });
            } else {
                setSelectedItem(combo, handle.getQualifiedName());
            }
        }
    });
}
 
Example 16
Source File: JComboBoxOptionJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private static Component getRendererComponent(JComboBox comboBox, int option) {
    ComboBoxModel model = comboBox.getModel();
    if (option >= model.getSize()) {
        throw new NoSuchElementException("Index out-of-bounds error on JComboBox: " + option, null);
    }
    Component rendererComponent = comboBox.getRenderer().getListCellRendererComponent(new JList(model),
            model.getElementAt(option), option, false, false);
    return rendererComponent;
}
 
Example 17
Source File: JPanel_FileBrowser.java    From MobyDroid with Apache License 2.0 4 votes vote down vote up
public ComboBoxAutoComplete(JComboBox<String> jComboBox) {
    this.jComboBox = jComboBox;
    this.defaultModel = jComboBox.getModel();
    this.documentUpdated = false;
}
 
Example 18
Source File: FeaturesSchemaEditor.java    From gate-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void prepareCombo(JComboBox combo, int row, int column){
  Feature feature = featureList.get(row);
  DefaultComboBoxModel comboModel = (DefaultComboBoxModel)combo.getModel(); 
  comboModel.removeAllElements();
  switch(column){
    case NAME_COL:
      List<String> fNames = new ArrayList<String>();
      if(schema != null && schema.getFeatureSchemaSet() != null){
        Iterator<FeatureSchema> fSchemaIter = schema.getFeatureSchemaSet().iterator();
        while(fSchemaIter.hasNext())
          fNames.add(fSchemaIter.next().getFeatureName());
      }
      if(!fNames.contains(feature.name))fNames.add(feature.name);
      Collections.sort(fNames);
      for(Iterator<String> nameIter = fNames.iterator(); 
          nameIter.hasNext(); 
          comboModel.addElement(nameIter.next()));
      combo.getEditor().getEditorComponent().setBackground(defaultBackground);          
      combo.setSelectedItem(feature.name);
      break;
    case VALUE_COL:
      List<Object> fValues = new ArrayList<Object>();
      if(feature.isSchemaFeature()){
        Set<Object> permValues = schema.getFeatureSchema(feature.name).
          getPermittedValues();
        if(permValues != null) fValues.addAll(permValues);
      }
      if(!fValues.contains(feature.value)) fValues.add(feature.value);
      Collections.sort(fValues, defaultComparator);
      for(Iterator<Object> valIter = fValues.iterator(); 
          valIter.hasNext(); 
          comboModel.addElement(valIter.next()));
      combo.getEditor().getEditorComponent().setBackground(feature.isCorrect() ?
          defaultBackground :
          (feature.isRequired() ? REQUIRED_WRONG : OPTIONAL_WRONG));
      combo.setSelectedItem(feature.value);
      break;
    default: ;
  }
  
}
 
Example 19
Source File: SummaryTablePanel.java    From nmonvisualizer with Apache License 2.0 4 votes vote down vote up
private void setupStatsPanel() {
    statsPanel.setVisible(false);

    JLabel label = new JLabel("Statistic:");
    label.setHorizontalAlignment(SwingConstants.TRAILING);
    label.setFont(Styles.LABEL);

    statsPanel.add(label);

    // Object and not Statistic to support displaying the granularity value
    JComboBox<Object> statistic = new JComboBox<Object>();

    DefaultComboBoxModel<Object> model = (DefaultComboBoxModel<Object>) statistic.getModel();
    model.addElement(Statistic.AVERAGE);
    model.addElement(Statistic.WEIGHTED_AVERAGE);
    model.addElement(Statistic.MINIMUM);
    model.addElement(Statistic.MAXIMUM);
    model.addElement(Statistic.GRANULARITY_MAXIMUM.getName(gui.getGranularity()));
    model.addElement(Statistic.STD_DEV);
    model.addElement(Statistic.MEDIAN);
    model.addElement(Statistic.SUM);
    model.addElement(Statistic.COUNT);

    statistic.setSelectedItem(Statistic.AVERAGE);

    statistic.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            @SuppressWarnings("unchecked")
            Object o = ((JComboBox<Object>) e.getSource()).getModel().getSelectedItem();

            if (o.getClass().equals(String.class)) {
                ((ByDataSetTableModel) dataSetTable.getModel()).setStatistic(Statistic.GRANULARITY_MAXIMUM);
            }
            else {
                ((ByDataSetTableModel) dataSetTable.getModel()).setStatistic((Statistic) o);
            }
        }
    });

    statsPanel.add(statistic);
}
 
Example 20
Source File: RepositoryComboSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void checkOldComboBoxModel(JComboBox comboBox) {
    ComboBoxModel oldModel = comboBox.getModel();
    if ((oldModel != null) && (oldModel.getSize() != 0)) {
        throw new IllegalStateException("The combo-box must be empty."); //NOI18N
    }
}