Example usage for javax.swing ComboBoxModel getSize

List of usage examples for javax.swing ComboBoxModel getSize

Introduction

In this page you can find the example usage for javax.swing ComboBoxModel getSize.

Prototype

int getSize();

Source Link

Document

Returns the length of the list.

Usage

From source file:MultiKeyCombo.java

public int selectionForKey(char aKey, ComboBoxModel aModel) {
    // Reset if invalid character
    if (aKey == KeyEvent.CHAR_UNDEFINED) {
        currentSearch.setLength(0);/*from   w  ww  . ja v a  2s. com*/
        return -1;
    }
    // Since search, don't reset search
    resetTimer.stop();
    // Convert input to uppercase
    char key = Character.toUpperCase(aKey);
    // Build up search string
    currentSearch.append(key);
    // Find selected position within model to starting searching from
    Object selectedElement = aModel.getSelectedItem();
    int selectedIndex = -1;
    if (selectedElement != null) {
        for (int i = 0, n = aModel.getSize(); i < n; i++) {
            if (aModel.getElementAt(i) == selectedElement) {
                selectedIndex = i;
                break;
            }
        }
    }
    boolean found = false;
    String search = currentSearch.toString();
    // Search from selected forward, wrap back to beginning if not found
    for (int i = 0, n = aModel.getSize(); i < n; i++) {
        String element = aModel.getElementAt(selectedIndex).toString().toUpperCase();
        if (element.startsWith(search)) {
            found = true;
            break;
        }
        selectedIndex++;
        if (selectedIndex == n) {
            selectedIndex = 0; // wrap
        }
    }
    // Restart timer
    resetTimer.start();
    return (found ? selectedIndex : -1);
}

From source file:edu.ku.brc.specify.prefs.FormattingPrefsPanel.java

/**
 * Create the UI for the panel/* w  w w. j a v  a  2  s .c  om*/
 */
protected void createUI() {
    createForm("Preferences", "Formatting"); //$NON-NLS-1$ //$NON-NLS-2$

    UIValidator.setIgnoreAllValidation(this, true);

    JLabel fontNamesLabel = form.getLabelFor("fontNames"); //$NON-NLS-1$
    ValComboBox fontNamesVCB = form.getCompById("fontNames"); //$NON-NLS-1$

    JLabel fontSizesLabel = form.getLabelFor("fontSizes"); //$NON-NLS-1$
    ValComboBox fontSizesVCB = form.getCompById("fontSizes"); //$NON-NLS-1$

    JLabel controlSizesLabel = form.getLabelFor("controlSizes"); //$NON-NLS-1$
    ValComboBox controlSizesVCB = form.getCompById("controlSizes"); //$NON-NLS-1$

    formTypesCBX = form.getCompById("formtype"); //$NON-NLS-1$

    fontNames = fontNamesVCB.getComboBox();
    fontSizes = fontSizesVCB.getComboBox();
    controlSizes = controlSizesVCB.getComboBox();

    testField = form.getCompById("fontTest"); //$NON-NLS-1$
    if (testField != null) {
        testField.setText(UIRegistry.getResourceString("FormattingPrefsPanel.THIS_TEST")); //$NON-NLS-1$
    }
    if (UIHelper.isMacOS_10_5_X()) {
        fontNamesLabel.setVisible(false);
        fontNamesVCB.setVisible(false);
        fontSizesLabel.setVisible(false);
        fontSizesVCB.setVisible(false);
        testField.setVisible(false);

        int inx = -1;
        int i = 0;
        Vector<String> controlSizeTitles = new Vector<String>();
        for (UIHelper.CONTROLSIZE cs : UIHelper.CONTROLSIZE.values()) {
            String titleStr = getResourceString(cs.toString());
            controlSizeTitles.add(titleStr);
            controlSizesHash.put(titleStr, cs);
            controlSizes.addItem(titleStr);
            if (cs == UIHelper.getControlSize()) {
                inx = i;
            }
            i++;
        }
        controlSizes.setSelectedIndex(inx);

        Font baseFont = UIRegistry.getBaseFont();
        if (baseFont != null) {
            fontNames.addItem(baseFont.getFamily());
            fontSizes.addItem(Integer.toString(baseFont.getSize()));
            fontNames.setSelectedIndex(0);
            fontSizes.setSelectedIndex(0);
        }

    } else {
        controlSizesLabel.setVisible(false);
        controlSizesVCB.setVisible(false);

        Hashtable<String, Boolean> namesUsed = new Hashtable<String, Boolean>();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        for (Font font : ge.getAllFonts()) {
            if (namesUsed.get(font.getFamily()) == null) {
                fontNames.addItem(font.getFamily());
                namesUsed.put(font.getFamily(), true); //$NON-NLS-1$
            }
        }
        for (int i = BASE_FONT_SIZE; i < 22; i++) {
            fontSizes.addItem(Integer.toString(i));
        }

        Font baseFont = UIRegistry.getBaseFont();
        if (baseFont != null) {
            fontNames.setSelectedItem(baseFont.getFamily());
            fontSizes.setSelectedItem(Integer.toString(baseFont.getSize()));

            if (testField != null) {
                ActionListener al = new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        testField.setFont(new Font((String) fontNames.getSelectedItem(), Font.PLAIN,
                                fontSizes.getSelectedIndex() + BASE_FONT_SIZE));
                        form.getUIComponent().validate();
                        clearFontSettings = false;
                    }
                };
                fontNames.addActionListener(al);
                fontSizes.addActionListener(al);
            }
        }
    }

    //-----------------------------------
    // Do DisciplineType Icons
    //-----------------------------------

    String iconName = AppPreferences.getRemote().get(getDisciplineImageName(), "CollectionObject"); //$NON-NLS-1$ //$NON-NLS-2$

    List<Pair<String, ImageIcon>> list = IconManager.getListByType("disciplines", IconManager.IconSize.Std16); //$NON-NLS-1$
    Collections.sort(list, new Comparator<Pair<String, ImageIcon>>() {
        public int compare(Pair<String, ImageIcon> o1, Pair<String, ImageIcon> o2) {
            String s1 = UIRegistry.getResourceString(o1.first);
            String s2 = UIRegistry.getResourceString(o2.first);
            return s1.compareTo(s2);
        }
    });

    disciplineCBX = (ValComboBox) form.getCompById("disciplineIconCBX"); //$NON-NLS-1$

    final JLabel dispLabel = form.getCompById("disciplineIcon"); //$NON-NLS-1$
    JComboBox comboBox = disciplineCBX.getComboBox();
    comboBox.setRenderer(new DefaultListCellRenderer() {
        @SuppressWarnings("unchecked") //$NON-NLS-1$
        public Component getListCellRendererComponent(JList listArg, Object value, int index,
                boolean isSelected, boolean cellHasFocus) {
            Pair<String, ImageIcon> item = (Pair<String, ImageIcon>) value;
            JLabel label = (JLabel) super.getListCellRendererComponent(listArg, value, index, isSelected,
                    cellHasFocus);
            if (item != null) {
                label.setIcon(item.second);
                label.setText(UIRegistry.getResourceString(item.first));
            }
            return label;
        }
    });

    int inx = 0;
    Pair<String, ImageIcon> colObj = new Pair<String, ImageIcon>("colobj_backstop", //$NON-NLS-1$
            IconManager.getIcon("colobj_backstop", IconManager.IconSize.Std16)); //$NON-NLS-1$
    comboBox.addItem(colObj);

    int cnt = 1;
    for (Pair<String, ImageIcon> item : list) {
        if (item.first.equals(iconName)) {
            inx = cnt;
        }
        comboBox.addItem(item);
        cnt++;
    }

    comboBox.addActionListener(new ActionListener() {
        @SuppressWarnings("unchecked") //$NON-NLS-1$
        public void actionPerformed(ActionEvent e) {
            JComboBox cbx = (JComboBox) e.getSource();
            Pair<String, ImageIcon> item = (Pair<String, ImageIcon>) cbx.getSelectedItem();
            if (item != null) {
                dispLabel.setIcon(IconManager.getIcon(item.first));
                form.getUIComponent().validate();
            }
        }
    });

    comboBox.setSelectedIndex(inx);

    //-----------------------------------
    // Date Field
    //-----------------------------------
    dateFieldCBX = form.getCompById("scrdateformat"); //$NON-NLS-1$
    fillDateFormat();

    //-----------------------------------
    // FormType
    //-----------------------------------
    fillFormTypes();

    //-----------------------------------
    // Do App Icon
    //-----------------------------------

    final JButton getIconBtn = form.getCompById("GetIconImage"); //$NON-NLS-1$
    final JButton clearIconBtn = form.getCompById("ClearIconImage"); //$NON-NLS-1$
    final JLabel appLabel = form.getCompById("appIcon"); //$NON-NLS-1$
    final JButton resetDefFontBtn = form.getCompById("ResetDefFontBtn"); //$NON-NLS-1$

    String imgEncoded = AppPreferences.getRemote().get(iconImagePrefName, ""); //$NON-NLS-1$
    ImageIcon innerAppImgIcon = null;
    if (StringUtils.isNotEmpty(imgEncoded)) {
        innerAppImgIcon = GraphicsUtils.uudecodeImage("", imgEncoded); //$NON-NLS-1$
        if (innerAppImgIcon != null && innerAppImgIcon.getIconWidth() != 32
                || innerAppImgIcon.getIconHeight() != 32) {
            innerAppImgIcon = null;
            clearIconBtn.setEnabled(false);
        } else {
            clearIconBtn.setEnabled(true);
        }
    }

    if (innerAppImgIcon == null) {
        innerAppImgIcon = IconManager.getIcon("AppIcon"); //$NON-NLS-1$
        clearIconBtn.setEnabled(false);
    } else {
        clearIconBtn.setEnabled(true);
    }
    appLabel.setIcon(innerAppImgIcon);

    getIconBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            chooseToolbarIcon(appLabel, clearIconBtn);
        }
    });

    clearIconBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ImageIcon appIcon = IconManager.getIcon("AppIcon");
            IconEntry entry = IconManager.getIconEntryByName(INNER_APPICON_NAME);
            entry.setIcon(appIcon);
            if (entry.getIcons().get(IconManager.IconSize.Std32) != null) {
                entry.getIcons().get(IconManager.IconSize.Std32).setImageIcon(appIcon);
            }

            appLabel.setIcon(IconManager.getIcon("AppIcon")); //$NON-NLS-1$
            clearIconBtn.setEnabled(false);
            AppPreferences.getRemote().remove(iconImagePrefName);
            form.getValidator().dataChanged(null, null, null);
        }
    });

    resetDefFontBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Font sysDefFont = UIRegistry.getDefaultFont();
            ComboBoxModel model = fontNames.getModel();
            for (int i = 0; i < model.getSize(); i++) {
                //System.out.println("["+model.getElementAt(i).toString()+"]["+sysDefFont.getFamily()+"]");
                if (model.getElementAt(i).toString().equals(sysDefFont.getFamily())) {
                    fontNames.setSelectedIndex(i);
                    clearFontSettings = true;
                    break;
                }
            }

            if (clearFontSettings) {
                fontSizes.setSelectedIndex(sysDefFont.getSize() - BASE_FONT_SIZE);
                clearFontSettings = true; // needs to be redone 
            }

            form.getValidator().dataChanged(null, null, null);
        }
    });

    //-----------------------------------
    // Do Banner Icon Size
    //-----------------------------------

    String fmtStr = "%d x %d pixels";//getResourceString("BNR_ICON_SIZE");
    bnrIconSizeCBX = form.getCompById("bnrIconSizeCBX"); //$NON-NLS-1$

    int size = AppPreferences.getLocalPrefs().getInt(BNR_ICON_SIZE, 20);
    inx = 0;
    cnt = 0;
    for (int pixelSize : pixelSizes) {
        ((DefaultComboBoxModel) bnrIconSizeCBX.getModel())
                .addElement(String.format(fmtStr, pixelSize, pixelSize));
        if (pixelSize == size) {
            inx = cnt;
        }
        cnt++;
    }

    bnrIconSizeCBX.getComboBox().addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            form.getUIComponent().validate();
        }
    });

    bnrIconSizeCBX.getComboBox().setSelectedIndex(inx);

    UIValidator.setIgnoreAllValidation(this, false);
    fontNamesVCB.setChanged(false);
    fontSizesVCB.setChanged(false);

    form.getValidator().validateForm();
}

From source file:io.github.jeddict.jpa.modeler.source.generator.ui.GenerateCodeDialog.java

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;
        }/*from  w w  w .ja v  a 2 s.  c o  m*/
    }
    ((JTextComponent) packageCombo.getEditor().getEditorComponent()).setText(_package);
}

From source file:io.github.jeddict.jpa.modeler.source.generator.ui.GenerateCodeDialog.java

private void populatePackageCombo(JComboBox packageCombo, ProjectInfo projectInfo) {
    if (projectInfo.getSourceGroup() != null) {
        packageCombo.setRenderer(PackageView.listRenderer());
        ComboBoxModel model = PackageView.createListView(projectInfo.getSourceGroup());
        if (model.getSize() > 0) {
            model.setSelectedItem(model.getElementAt(0));
        }//from ww w  . j  av a  2  s .  c o m
        packageCombo.setModel(model);
    }
}

From source file:gdt.jgui.entity.query.JQueryPanel.java

private void setSelection(JComboBox<String> comboBox, String item$) {
    ComboBoxModel<String> model = comboBox.getModel();
    if (model != null) {
        int cnt = model.getSize();
        String[] sa = null;/*from   w w  w  .  j av  a 2s.co  m*/
        if (cnt > 0) {
            sa = new String[cnt];
            for (int i = 0; i < cnt; i++)
                if (item$.equals(model.getElementAt(i))) {
                    comboBox.setSelectedIndex(i);
                    return;
                }
        }
    }
}

From source file:edu.ku.brc.specify.tasks.subpane.wb.FormPane.java

protected Vector<Object> getValues(final WorkbenchTemplateMappingItem wbtmi) {
    Vector<WorkbenchTemplateMappingItem> wbtmis = new Vector<WorkbenchTemplateMappingItem>();
    wbtmis.addAll(workbench.getWorkbenchTemplate().getWorkbenchTemplateMappingItems());
    Collections.sort(wbtmis);//from ww w . j av a2s  .  co m
    int wbCol = -1;
    for (int c = 0; c < wbtmis.size(); c++) {
        if (wbtmis.get(c) == wbtmi) {
            wbCol = c;
            break;
        }
    }
    if (wbCol != -1) {
        if (workbenchPane.getSpreadSheet().getColumnModel().getColumn(wbCol)
                .getCellEditor() instanceof WorkbenchPaneSS.GridCellListEditor) {
            ComboBoxModel model = ((WorkbenchPaneSS.GridCellListEditor) workbenchPane.getSpreadSheet()
                    .getColumnModel().getColumn(wbCol).getCellEditor()).getList();
            Vector<Object> result = new Vector<Object>();
            for (int i = 0; i < model.getSize(); i++) {
                result.add(model.getElementAt(i));
            }
            return result;
        }
    }
    return null;
}

From source file:org.apache.jmeter.protocol.java.config.gui.JavaConfigGui.java

/**
 * Check combo contains className/*from w  w  w. j  a  v  a2 s . c o  m*/
 * @param model ComboBoxModel
 * @param className String class name
 * @return boolean
 */
private static boolean checkContainsClassName(ComboBoxModel<String> model, String className) {
    int size = model.getSize();
    Set<String> set = new HashSet<>(size);
    for (int i = 0; i < size; i++) {
        set.add(model.getElementAt(i));
    }
    return set.contains(className);
}

From source file:org.apache.jmeter.visualizers.backend.BackendListenerGui.java

/**
 * Check combo contains className/* w  w  w. ja v a  2  s  .c  o m*/
 * @param model ComboBoxModel
 * @param className String class name
 * @return boolean true if model contains className
 */
private static boolean checkContainsClassName(ComboBoxModel<?> model, String className) {
    int size = model.getSize();
    Set<String> set = new HashSet<>(size);
    for (int i = 0; i < size; i++) {
        set.add((String) model.getElementAt(i));
    }
    return set.contains(className);
}

From source file:org.eclipse.wb.internal.swing.model.property.editor.models.combo.ComboBoxModelPropertyEditor.java

/**
 * @return the items specified in value of given {@link Property}.
 *///  w ww .jav a 2s . c  o m
private static String[] getItems(Property property) throws Exception {
    Object value = property.getValue();
    if (value instanceof ComboBoxModel) {
        List<String> items = Lists.newArrayList();
        ComboBoxModel model = (ComboBoxModel) value;
        for (int i = 0; i < model.getSize(); i++) {
            Object element = model.getElementAt(i);
            if (element instanceof String) {
                items.add((String) element);
            }
        }
        return items.toArray(new String[items.size()]);
    }
    // no items
    return ArrayUtils.EMPTY_STRING_ARRAY;
}

From source file:org.gcaldaemon.gui.config.MainConfig.java

public static final boolean selectItem(JComboBox combo, String value) {
    if (value == null || combo == null || value.length() == 0) {
        return false;
    }/*w  ww .  java 2 s. c  om*/
    ComboBoxModel model = combo.getModel();
    if (model != null) {
        int n, size = model.getSize();
        n = value.indexOf(" - ");
        if (n != -1) {
            value = value.substring(n + 3);
        }
        String test;
        for (int i = 0; i < size; i++) {
            test = (String) model.getElementAt(i);
            if (test != null) {
                n = test.indexOf(" - ");
                if (n != -1) {
                    test = test.substring(n + 3);
                }
                if (test.equals(value)) {
                    combo.setSelectedIndex(i);
                    combo.setToolTipText((String) model.getElementAt(i));
                    return true;
                }
            }
        }
    }
    if (combo.isEditable()) {
        combo.setSelectedItem(value);
        combo.setToolTipText(value);
        return true;
    }
    return false;
}