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

The following examples show how to use javax.swing.JComboBox#getItemAt() . 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: MemSearchAsciiTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void setEncoding(Charset encoding) throws Exception {
	JComboBox<Charset> encodingOptions =
		(JComboBox<Charset>) findComponentByName(pane, "Encoding Options", false);

	// Makes encoding UTF_16 in case encoding is UTF_16BE or UTF_16LE
	// BE and LE are not choices in the combo box.
	if (encoding == StandardCharsets.UTF_16BE || encoding == StandardCharsets.UTF_16LE) {
		encoding = StandardCharsets.UTF_16;
	}

	for (int i = 0; i < encodingOptions.getItemCount(); i++) {
		if (encodingOptions.getItemAt(i) == encoding) {
			int index = i;
			runSwing(() -> encodingOptions.setSelectedIndex(index));
			break;
		}
	}
}
 
Example 2
Source File: WizardUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Searches LayerItemPresenter combobox by the item's display name.
 */
private static String searchLIPCategoryCombo(final JComboBox lpCombo, final String displayName) {
    String path = null;
    for (int i = 0; i < lpCombo.getItemCount(); i++) {
        Object item = lpCombo.getItemAt(i);
        if (!(item instanceof LayerItemPresenter)) {
            continue;
        }
        LayerItemPresenter presenter = (LayerItemPresenter) lpCombo.getItemAt(i);
        if (displayName.equals(presenter.getDisplayName())) {
            path = presenter.getFullPath();
            break;
        }
    }
    return path;
}
 
Example 3
Source File: DatabaseExplorerInternalUIsTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testComboboxWithDrivers() throws Exception {
    setUpDrivers();
    JComboBox combo = new JComboBox();
    DatabaseExplorerInternalUIs.connect(combo, JDBCDriverManager.getDefault());

    assertEquals(3, combo.getItemCount());
    JdbcUrl url = (JdbcUrl)combo.getItemAt(0);
    assertDriversEqual(driver2, url.getDriver());
    assertEquals(driver2.getClassName(), url.getClassName());
    assertEquals(driver2.getDisplayName(), url.getDisplayName());
    
    url = (JdbcUrl)combo.getItemAt(1);
    assertDriversEqual(driver1, url.getDriver());
    assertEquals(driver1.getClassName(), url.getClassName());
    assertEquals(driver1.getDisplayName(), url.getDisplayName());
}
 
Example 4
Source File: JComboBoxOperator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns information about component.
 */
@Override
public Hashtable<String, Object> getDump() {
    Hashtable<String, Object> result = super.getDump();
    JComboBox<?> jComboBox = (JComboBox<?>) getSource();
    Object selectedItem = jComboBox.getSelectedItem();
    if (selectedItem != null) {
        result.put(TEXT_DPROP, selectedItem.toString());
    }
    int itemCount = jComboBox.getItemCount();
    String[] items = new String[itemCount];
    for (int i = 0; i < itemCount; i++) {
        if (jComboBox.getItemAt(i) != null) {
            items[i] = jComboBox.getItemAt(i).toString();
        }
    }
    addToDump(result, ITEM_PREFIX_DPROP, items);
    return result;
}
 
Example 5
Source File: DBConnectionUtil.java    From jeddict with Apache License 2.0 6 votes vote down vote up
/**
 * Load combobox with DB connection
 *
 * @param file
 * @param dbConComboBox
 */
public static void loadConnection(ModelerFile file, JComboBox dbConComboBox) {
    EntityMappings entityMappings = (EntityMappings) file.getDefinitionElement();
    Cache cache = entityMappings.getCache();
    DatabaseConnectionCache dbCache = cache.getDatabaseConnectionCache();

    DatabaseExplorerUIs.connect(dbConComboBox, ConnectionManager.getDefault());
    dbConComboBox.setToolTipText("Available Database Connection");

    for (int i = 0; i < dbConComboBox.getItemCount(); i++) {
        Object item = dbConComboBox.getItemAt(i);
        if (dbCache != null && item instanceof DatabaseConnection && ((DatabaseConnection) item).getDatabaseURL().equals(dbCache.getUrl())) {
            dbConComboBox.setSelectedIndex(i);
            break;
        }
    }
 
}
 
Example 6
Source File: EditTeamService.java    From Shuffle-Move with GNU General Public License v3.0 6 votes vote down vote up
private void updateKeybindComboBoxes() {
   Team curTeam = getCurrentTeam();
   for (String name : curTeam.getNames()) {
      ItemListener itemListener = nameToItemListenerMap.get(name);
      JComboBox<Character> box = nameToKeybindComboboxMap.get(name);
      box.removeItemListener(itemListener);
      Character prevSelected = box.getItemAt(box.getSelectedIndex());
      box.removeAllItems();
      Character curBinding = curTeam.getBinding(name);
      LinkedHashSet<Character> availableBindings = new LinkedHashSet<Character>(Arrays.asList(curBinding));
      availableBindings.addAll(myData.getAllAvailableBindingsFor(name, curTeam));
      for (Character ch : availableBindings) {
         if (ch != null) {
            box.addItem(ch);
         }
      }
      box.setSelectedItem(prevSelected);
      if (box.getSelectedIndex() < 0) {
         LOG.warning(getString(KEY_NO_BINDINGS));
      }
      box.addItemListener(itemListener);
   }
}
 
Example 7
Source File: DesignerTablePanel.java    From nextreports-designer with Apache License 2.0 6 votes vote down vote up
public void updateGroupByItems(int dragRow, int dropRow) {
    MyRow drag_row = (MyRow) model.getObjectForRow(dragRow);
    JComboBox groupByComboDrag = (JComboBox) ((DefaultCellEditor) table.getCellEditor(dragRow, 6)).getComponent();

    MyRow drop_row = (MyRow) model.getObjectForRow(dropRow);
    JComboBox groupByComboDrop = (JComboBox) ((DefaultCellEditor) table.getCellEditor(dropRow, 6)).getComponent();

    String sDrag = (String) groupByComboDrag.getItemAt(0);
    String sDrop = (String) groupByComboDrop.getItemAt(0);

    if ("".equals(sDrag) && !sDrag.equals(sDrop)) {
        groupByComboDrop.insertItemAt("", 0);
        groupByComboDrag.removeItem("");
    }

    if ("".equals(sDrop) && !sDrop.equals(sDrag)) {
        groupByComboDrag.insertItemAt("", 0);
        groupByComboDrop.removeItem("");
    }
}
 
Example 8
Source File: ConnectionFailedTask.java    From JPPF with Apache License 2.0 5 votes vote down vote up
/**
 * Perform the task.
 */
@Override
public void run() {
  synchronized(statsHandler) {
    if (statsHandler.dataHolderMap.get(driver.getUuid()) != null) {
      final ConnectionDataHolder cdh = statsHandler.dataHolderMap.remove(driver.getUuid());
      if (cdh != null) cdh.close();
    }
  }
  JComboBox<?> box = null;
  while (statsHandler.getClientHandler().getServerListOption() == null) goToSleep(50L);
  final JPPFClientConnection c = driver.getConnection();
  synchronized(statsHandler) {
    if (debugEnabled) log.debug("removing client connection " + c.getName() + " from driver combo box");
    box = ((ComboBoxOption) statsHandler.getClientHandler().getServerListOption()).getComboBox();
    final int count = box.getItemCount();
    int idx = -1;
    for (int i=0; i<count; i++) {
      final Object o = box.getItemAt(i);
      if (c.equals(o)) {
        box.removeItemAt(i);
        idx = i;
        break;
      }
    }
    if ((idx >= 0) && (box.getItemCount() > 0)) {
      if ((statsHandler.getClientHandler().currentDriver == null) || c.equals(statsHandler.getClientHandler().currentDriver.getConnection())) {
        final int n = Math.min(idx, box.getItemCount()-1);
        final TopologyDriver item = (TopologyDriver) box.getItemAt(n);
        statsHandler.getClientHandler().currentDriver = item;
        box.setSelectedItem(item);
      }
    }
  }
}
 
Example 9
Source File: PersistenceUnitPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the selected item in connection combo box.
 */
private void setSelectedConnection(){
    DatabaseConnection connection = ProviderUtil.getConnection(persistenceUnit);
    if (connection != null){
        jdbcComboBox.setSelectedItem(connection);
    } else {
        // custom connection (i.e. connection not registered in netbeans)
        Properties props = persistenceUnit.getProperties();
        if (props != null){
            Property[] properties = props.getProperty2();
            String url = null;
            ArrayList<Provider> providers = new ArrayList<Provider>();
            JComboBox activeCB = providerCombo.isVisible() ? providerCombo : libraryComboBox;
            for(int i=0; i<activeCB.getItemCount(); i++){
                Object obj = activeCB.getItemAt(i);
                if(obj instanceof Provider){
                    providers.add((Provider) obj);
                }
            }
            Provider provider = ProviderUtil.getProvider(persistenceUnit, providers.toArray(new Provider[]{}));
            for (int i = 0; i < properties.length; i++) {
                String key = properties[i].getName();
                if (provider.getJdbcUrl().equals(key)) {
                    url = properties[i].getValue();
                    break;
                }
            }
            if (url == null) {
                url = NbBundle.getMessage(PersistenceUnitPanel.class, "LBL_CustomConnection");//NOI18N
            }
            jdbcComboBox.addItem(url);
            jdbcComboBox.setSelectedItem(url);
        }
    }
}
 
Example 10
Source File: SearchPatternController.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Bind Match Type option to a combo box.
 *
 * @param comboBox Combo box to control and display the match type. The
 * model of the combo box can contain only items of type {@link MatchType}.
 * {@link MatchType#LITERAL} and {@link MatchType#REGEXP} are mandatory in
 * the model.
 *
 * @since api.search/1.11
 */
public void bindMatchTypeComboBox(@NonNull final JComboBox comboBox) {
    Parameters.notNull("comboBox", comboBox);                       //NOI18N

    boolean regexpFound = false, literalFound = false;
    for (int i = 0; i < comboBox.getItemCount(); i++) {
        if (comboBox.getItemAt(i) == MatchType.LITERAL) {
            literalFound = true;
        } else if (comboBox.getItemAt(i) == MatchType.REGEXP) {
            regexpFound = true;
        } else if (!(comboBox.getItemAt(i) instanceof MatchType)) {
            throw new IllegalArgumentException("Model of the combo "//NOI18N
                    + "box can contain only MatchType items");      //NOI18N
        }
    }
    if (!(regexpFound && literalFound)) {
        throw new IllegalArgumentException(
                "At least MatchType.LITERAL and MatchType.REGEXP " //NOI18N
                + "must be contained in the combo box model.");     //NOI18N
    }
    if (matchTypeComboBox != null) {
        throw new IllegalStateException(
                "Already bound with option MATCH_TYPE");            //NOI18N
    }
    this.matchTypeComboBox = comboBox;
    comboBox.setEditable(false);
    setMatchType(this.matchType); //update match type, check it is supported
    comboBox.setSelectedItem(matchType);
    comboBox.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            setMatchType((MatchType) comboBox.getSelectedItem());
        }
    });
}
 
Example 11
Source File: DatabaseExplorerInternalUIsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testComboboxWithDriversOfSameClass() throws Exception {
    removeDrivers();

    String name1 = "foo_driver";
    String name2 = "foo_driver2";

    String displayName1 = "FooDriver";
    String displayName2 = "FooDriver2";

    driver1 = JDBCDriver.create(name1, displayName1, "org.foo.FooDriver", new URL[0]);
    JDBCDriverManager.getDefault().addDriver(driver1);

    driver2 = JDBCDriver.create(name2, displayName2, "org.foo.FooDriver", new URL[0]);
    JDBCDriverManager.getDefault().addDriver(driver2);

    JComboBox combo = new JComboBox();
    DatabaseExplorerInternalUIs.connect(combo, JDBCDriverManager.getDefault());

    assertEquals(3, combo.getItemCount());

    JdbcUrl url = (JdbcUrl)combo.getItemAt(0);
    assertDriversEqual(driver1, url.getDriver());
    assertEquals(driver1.getClassName(), url.getClassName());
    assertEquals(driver1.getDisplayName(), url.getDisplayName());
    assertEquals(driver1.getName(), url.getName());

    url = (JdbcUrl)combo.getItemAt(1);
    assertDriversEqual(driver2, url.getDriver());
    assertEquals(driver2.getClassName(), url.getClassName());
    assertEquals(driver2.getDisplayName(), url.getDisplayName());
    assertEquals(driver2.getName(), url.getName());
}
 
Example 12
Source File: DatabaseExplorerInternalUIsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testComboBoxWithDriverClass() throws Exception {
    setUpDrivers();
    JComboBox combo = new JComboBox();
    DatabaseExplorerInternalUIs.connect(combo, JDBCDriverManager.getDefault(), "org.bar.BarDriver");

    assertEquals(1, combo.getItemCount());
    JdbcUrl url = (JdbcUrl)combo.getItemAt(0);
    assertDriversEqual(driver2, url.getDriver());
    assertEquals(driver2.getClassName(), url.getClassName());
    assertEquals(driver2.getDisplayName(), url.getDisplayName());
}
 
Example 13
Source File: SelectAmountDialog.java    From freecol with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verify the contents of the JComboBox.
 *
 * @param box The {@code JComboBox} to verify.
 * @return True if all is well.
 */
private boolean verifyWholeBox(JComboBox<Integer> box) {
    final int n = box.getItemCount();
    for (int i = 0; i < n; i++) {
        Integer v = box.getItemAt(i);
        if (v < 0 || v > available) return false;
    }
    return true;
}
 
Example 14
Source File: PrefOption.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
static <Option> void setSelected(JComboBox<Option> combo, Object value) {
	for (int i = combo.getItemCount() - 1; i >= 0; i--) {
		PrefOption opt = (PrefOption) combo.getItemAt(i);
		if (opt.getValue().equals(value)) {
			combo.setSelectedItem(opt);
			return;
		}
	}
	combo.setSelectedItem(combo.getItemAt(0));
}
 
Example 15
Source File: ComboOption.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
static void setSelected(JComboBox<?> combo, Object value) {
	for (int i = combo.getItemCount() - 1; i >= 0; i--) {
		ComboOption opt = (ComboOption) combo.getItemAt(i);
		if (opt.getValue().equals(value)) {
			combo.setSelectedItem(opt);
			return;
		}
	}
	combo.setSelectedItem(combo.getItemAt(0));
}
 
Example 16
Source File: UIUtilities.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Since JComboBox.getSelectedItem() returns a plain Object, this allows us to get the
 * appropriate type of object instead.
 */
public static <E> E getTypedSelectedItemFromCombo(JComboBox<E> combo) {
    int index = combo.getSelectedIndex();
    return index == -1 ? null : combo.getItemAt(index);
}