Example usage for javax.swing DefaultComboBoxModel getElementAt

List of usage examples for javax.swing DefaultComboBoxModel getElementAt

Introduction

In this page you can find the example usage for javax.swing DefaultComboBoxModel getElementAt.

Prototype

public E getElementAt(int index) 

Source Link

Usage

From source file:Main.java

public static void main(final String args[]) {
    final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();

    model.addElement("A");
    model.addElement("C");
    model.addElement("D");
    model.addElement("A");

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox<String> comboBox1 = new JComboBox<String>(model);
    comboBox1.setMaximumRowCount(5);//from   w ww  . ja  v  a2s  .c o m
    comboBox1.setEditable(true);
    frame.add(comboBox1, BorderLayout.NORTH);

    JList<String> jlist = new JList<String>(model);
    JScrollPane scrollPane = new JScrollPane(jlist);
    frame.add(scrollPane, BorderLayout.CENTER);

    JButton button = new JButton("Add");
    frame.add(button, BorderLayout.SOUTH);
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.addElement("a");
            System.out.println(model.getElementAt(1));
        }
    };
    button.addActionListener(actionListener);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:edu.ku.brc.specify.datamodel.busrules.PickListBusRules.java

/**
 * Returns the index in the model for the string value.
 * @param vcbx the combobox//from   www  . j  a v  a 2 s  . c om
 * @param value the value
 * @return the index in the model for the string value
 */
private int getIndexInModel(final ValComboBox vcbx, final String value) {
    DefaultComboBoxModel model = (DefaultComboBoxModel) vcbx.getComboBox().getModel();
    for (int i = 0; i < model.getSize(); i++) {
        Object obj = model.getElementAt(i);
        if (obj instanceof DBTableInfo) {
            DBTableInfo ti = (DBTableInfo) obj;
            if (ti.getName().equals(value)) {
                return i;
            }

        } else if (obj instanceof DBFieldInfo) {
            DBFieldInfo fi = (DBFieldInfo) obj;
            if (fi.getName().equals(value)) {
                return i;
            }

        } else if (obj instanceof DataObjSwitchFormatter) {
            DataObjSwitchFormatter fmt = (DataObjSwitchFormatter) obj;
            if (fmt.getName().equals(value)) {
                return i;
            }
        } else if (obj.toString().equals(value)) {
            return i;
        }
    }
    return -1;
}

From source file:com.mirth.connect.client.ui.panels.export.MessageExportPanel.java

public void setMessageWriterOptions(MessageWriterOptions options) {
    if (options.getContentType() == null) {
        contentComboBox.setSelectedItem(XML_EXPORT_FORMAT);
    } else {//  w w w. j a  v  a 2  s . c o m
        DefaultComboBoxModel model = (DefaultComboBoxModel) contentComboBox.getModel();

        for (int i = 0; i < model.getSize(); i++) {
            Object element = model.getElementAt(i);

            if (element instanceof ExportFormat) {
                ExportFormat exportFormat = (ExportFormat) element;

                if (exportFormat.getContentType().equals(options.getContentType())
                        && exportFormat.isDestination() == options.isDestinationContent()) {
                    contentComboBox.setSelectedItem(exportFormat);
                }
            }
        }
    }

    encryptCheckBox.setSelected(options.isEncrypt());

    ArchiveFormat archiveFormat = ArchiveFormat.lookup(options.getArchiveFormat(), options.getCompressFormat());

    if (archiveFormat == null) {
        compressComboBox.setSelectedItem(NO_COMPRESSION);
    } else {
        compressComboBox.setSelectedItem(archiveFormat);
    }

    compressComboBoxChanged();

    rootPathTextField.setText(options.getRootFolder());
    filePatternTextPane.setText(options.getFilePattern());

    repaint();
}

From source file:com.mirth.connect.client.ui.panels.connectors.ResponseSettingsPanel.java

private void setSelectedItem(Object selectedItem) {
    DefaultComboBoxModel model = (DefaultComboBoxModel) responseComboBox.getModel();

    if (selectedItem instanceof Entry) {
        /*/*from   w  ww .ja  va 2s . c om*/
         * If the selected item is an Entry and the key ("d#") is the same
         * as an Entry in the model, then they're "the same", and that entry
         * is selected.
         */
        for (int i = 0; i <= model.getSize() - 1; i++) {
            if (model.getElementAt(i) instanceof Entry && ((Entry<String, String>) selectedItem).getKey()
                    .equals(((Entry<String, String>) model.getElementAt(i)).getKey())) {
                responseComboBox.setSelectedIndex(i);
                return;
            }
        }
    } else if (model.getIndexOf(selectedItem) >= 0) {
        responseComboBox.setSelectedItem(selectedItem);
        return;
    }
    responseComboBox.setSelectedIndex(0);
}

From source file:com.mirth.connect.client.ui.panels.connectors.SourceSettingsPanel.java

private void setSelectedItem(Object selectedItem) {
    DefaultComboBoxModel model = (DefaultComboBoxModel) responseComboBox.getModel();

    if (selectedItem instanceof Entry) {
        /*/*from   w  ww .  j  a v a  2  s  .co  m*/
         * If the selected item is an Entry and the key ("d#") is the same as an Entry in the
         * model, then they're "the same", and that entry is selected.
         */
        for (int i = 0; i <= model.getSize() - 1; i++) {
            if (model.getElementAt(i) instanceof Entry && ((Entry<String, String>) selectedItem).getKey()
                    .equals(((Entry<String, String>) model.getElementAt(i)).getKey())) {
                responseComboBox.setSelectedIndex(i);
                return;
            }
        }
    } else if (model.getIndexOf(selectedItem) >= 0) {
        responseComboBox.setSelectedItem(selectedItem);
        return;
    }
    responseComboBox.setSelectedIndex(0);
}

From source file:com.raddle.tools.MergeMain.java

private void selectLine(JList list, PropertyLine line) {
    DefaultComboBoxModel model = (DefaultComboBoxModel) list.getModel();
    for (int i = 0; i < model.getSize(); i++) {
        PropertyLine mLine = (PropertyLine) model.getElementAt(i);
        if (mLine.getKey().equals(line.getKey())) {
            list.setSelectedValue(mLine, true);
            return;
        }// w  w w . ja  va 2s.co  m
    }
}

From source file:edu.ku.brc.specify.tools.schemalocale.FieldItemPanel.java

/**
 * // ww w.j  a  v  a 2  s  .co m
 */
private void setAsDefFormatter() {
    Object item = formatCombo.getSelectedItem();
    UIFieldFormatterIFace selected = null;
    if (item instanceof UIFieldFormatterIFace) {
        selected = (UIFieldFormatterIFace) item;
    }
    DefaultComboBoxModel model = (DefaultComboBoxModel) formatCombo.getModel();
    for (int i = 1; i < model.getSize(); i++) {
        UIFieldFormatterIFace uif = (UIFieldFormatterIFace) model.getElementAt(i);
        uif.setDefault(uif == selected);
    }
    if (fieldInfo != null) {
        fieldInfo.setFormatter(selected);
    }
}

From source file:edu.ku.brc.specify.tools.schemalocale.FieldItemPanel.java

/**
 * Fills the format Combobox with the available formatters.
 *//*from   w ww.j a  v  a 2  s .  c om*/
protected void fillWebLinkBox() {
    if (webLinkCombo != null) {
        DefaultComboBoxModel wlModel = (DefaultComboBoxModel) webLinkCombo.getModel();
        wlModel.removeAllElements();
        //if (wlModel.getSize() == 0)
        {
            wlModel.addElement(webLinkDefNone);
            for (WebLinkDef wld : webLinkMgrCache.getWebLinkDefs(null)) {
                wlModel.addElement(wld);
            }
        }

        //wbLnkCombo.setEnabled(false);
        webLinkMoreBtn.setEnabled(true);
        //wbLnkCombo.setEnabled(false);

        int selInx = webLinkCombo.getModel().getSize() > 0 ? 0 : -1;
        LocalizableItemIFace fld = getSelectedFieldItem();
        if (fld != null) {
            String webLinkName = fld.getWebLinkName();
            if (StringUtils.isNotEmpty(webLinkName)) {
                DefaultComboBoxModel model = (DefaultComboBoxModel) webLinkCombo.getModel();
                for (int i = 0; i < model.getSize(); i++) {
                    WebLinkDef wld = (WebLinkDef) model.getElementAt(i);
                    if (wld.getName().equals(webLinkName)) {
                        selInx = i;
                        break;
                    }
                }
            }
        }
        webLinkCombo.setSelectedIndex(selInx);
    }
}

From source file:com.alvermont.terraj.planet.ui.MainFrame.java

/**
 * Set up our list model from the projection list
 *//*from w ww  .j a v  a 2  s .c  om*/
protected void setListModel() {
    final DefaultComboBoxModel<Projector> dcm = new DefaultComboBoxModel<>();

    for (Projector p : projManager.getProjections()) {
        dcm.addElement(p);
    }

    projComboBox.setModel(dcm);

    // make the first one selected
    dcm.setSelectedItem(dcm.getElementAt(0));

    // TODO: do this properly
    projComboBoxActionPerformed(new ActionEvent(this, 0, "Hack"));
}

From source file:edu.ku.brc.specify.tools.schemalocale.FieldItemPanel.java

/**
 * /*from w  ww  .  j  av a 2  s  . com*/
 */
public void buildUI() {
    setIgnoreChanges(true);

    fieldsList = createList(fieldsModel);

    fieldsList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                getAllDataFromUI();
                fieldSelected();
            }
        }
    });

    fieldsList.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            super.focusLost(e);
            //lastIndex = fieldsList.getSelectedIndex();
        }
    });

    fieldDescText.addKeyListener(new LengthWatcher(255));
    // setting min and pref sizes to some bogus values so that textarea shrinks with dialog
    fieldNameText.addKeyListener(new LengthWatcher(64));

    CellConstraints cc = new CellConstraints();

    int y = 1;

    JScrollPane fldsp = UIHelper.createScrollPane(fieldsList);

    // LocalizableNameDescIFace
    PanelBuilder pb = new PanelBuilder(new FormLayout("max(200px;p),4px,p,2px,p,10px,p,2px,p,f:p:g",
            (includeHiddenUI ? "p,2px," : "") + (isDBSchema ? "p,2px,p,2px," : "")
                    + (includeFormatAndAutoNumUI ? "p,2px,p,2px," : "")
                    + "p,2px,p,2px,p,2px,p,2px,p,2px,p,2px,f:p:g"),
            this);

    pb.add(fldsp, cc.xywh(1, y, 1, 7 + (isDBSchema ? 4 : 0)));
    pb.add(fieldNameLbl = createI18NFormLabel("SL_LABEL", SwingConstants.RIGHT), cc.xy(3, y));
    pb.add(fieldNameText, cc.xywh(5, y, 6, 1));
    y += 2;

    if (includeHiddenUI) {
        pb.add(fieldHideChk, cc.xy(5, y));
        y += 2;
    }

    pb.add(fieldDescLbl = createI18NFormLabel("SL_DESC", SwingConstants.RIGHT), cc.xy(3, y));
    JScrollPane sp = new JScrollPane(fieldDescText, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    pb.add(sp, cc.xywh(5, y, 6, 1));
    y += 2;
    fieldDescText.setLineWrap(true);
    fieldDescText.setWrapStyleWord(true);

    if (isDBSchema) {
        fieldTypeTxt = createLabel("");
        fieldLengthTxt = createLabel("");

        pb.add(fieldTypeLbl = createI18NFormLabel("SL_TYPE", SwingConstants.RIGHT), cc.xy(3, y));
        pb.add(fieldTypeTxt, cc.xy(5, y));

        pb.add(fieldReqChk = createCheckBox(getResourceString("SL_REQ")), cc.xy(9, y));
        y += 2;

        pb.add(fieldLengthLbl = createI18NFormLabel("SL_LENGTH", SwingConstants.RIGHT), cc.xy(3, y));
        pb.add(fieldLengthTxt, cc.xy(5, y));
        y += 2;

        fieldTypeTxt.setBackground(Color.WHITE);
        fieldLengthTxt.setBackground(Color.WHITE);
        fieldTypeTxt.setOpaque(true);
        fieldLengthTxt.setOpaque(true);
    }

    if (includeFormatAndAutoNumUI) {
        PanelBuilder inner = new PanelBuilder(new FormLayout("p,2px,p", "p"));

        formatSwitcherCombo = createComboBox();
        fmtCardLayout = new CardLayout();
        formatterPanel = new JPanel(fmtCardLayout);
        pb.add(formatLbl = createI18NFormLabel("SL_FMTTYPE", SwingConstants.RIGHT), cc.xy(3, y));

        inner.add(formatSwitcherCombo, cc.xy(1, 1));
        inner.add(formatterPanel, cc.xy(3, 1));

        pb.add(inner.getPanel(), cc.xywh(5, y, 6, 1));
        y += 2;

        ActionListener switchAL = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String item = (String) formatSwitcherCombo.getSelectedItem();
                if (item != null) {
                    fmtCardLayout.show(formatterPanel, item);
                    webLinkCombo.setEnabled(item.equals(SL_WEBLINK));

                    if (formatSwitcherCombo.getSelectedIndex() == 0) {
                        if (webLinkCombo.getModel().getSize() > 0) {
                            webLinkCombo.setSelectedIndex(0);
                        }
                        if (formatCombo.getModel().getSize() > 0) {
                            formatCombo.setSelectedIndex(0);
                        }
                        if (pickListCBX.getModel().getSize() > 0) {
                            pickListCBX.setSelectedIndex(0);
                        }
                    }
                }
            }
        };
        formatSwitcherCombo.addActionListener(switchAL);

        formatterPanel.add(SL_NONE, new JPanel());

        //--------------------------
        // UIFieldFormatter
        //--------------------------
        inner = new PanelBuilder(new FormLayout("max(p;150px),2px,min", "p"));

        formatCombo = createComboBox(new DefaultComboBoxModel());
        formatMoreBtn = createButton(ELIPSES);

        inner.add(formatCombo, cc.xy(1, 1));
        inner.add(formatMoreBtn, cc.xy(3, 1));

        formatMoreBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                UIFormatterListEdtDlg dlg = new UIFormatterListEdtDlg((Frame) UIRegistry.getTopWindow(),
                        fieldInfo, true, schemaPanel.getUiFieldFormatterMgrCache());
                dlg.setVisible(true);
                if (!dlg.isCancelled() && dlg.hasChanged()) {
                    //schemaPanel.setHasChanged(true);
                    formHasChanged();

                    //fillFormatBox(dlg.getSelectedFormat());
                    setSelectedFieldFormatter(dlg.getSelectedFormat());
                }
            }
        });

        formatterPanel.add(SL_FORMAT, inner.getPanel());

        ActionListener changed = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                formHasChanged();

                boolean hasFormat = formatCombo.getSelectedIndex() > 0;
                webLinkCombo.setEnabled(!hasFormat);
                if (hasFormat) {
                    webLinkCombo.setSelectedIndex(webLinkCombo.getModel().getSize() > 0 ? 0 : -1);
                    pickListCBX.setSelectedIndex(pickListCBX.getModel().getSize() > 0 ? 0 : -1);
                }
            }
        };
        formatCombo.addActionListener(changed);

        //--------------------------
        // WebLinks
        //--------------------------
        webLinkMoreBtn = createButton(ELIPSES);
        webLinkMoreBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                WebLinkDef selectedWL = (WebLinkDef) webLinkCombo.getSelectedItem();
                WebLinkConfigDlg dlg = webLinkMgrCache.editWebLinks(tableInfo, false);
                if (dlg.getBtnPressed() == CustomDialog.OK_BTN) {
                    fillWebLinkBox();

                    formHasChanged();

                    if (selectedWL != null && !selectedWL.getName().equals(SL_WEBLINK)) {
                        dlg.setWebLink(selectedWL.getName());
                    }
                    if (dlg.getBtnPressed() == CustomDialog.OK_BTN) {
                        setSelectedWebLink(dlg.getSelectedItem());
                    }
                }
            }
        });

        inner = new PanelBuilder(new FormLayout("max(p;150px),2px,min", "p"));
        webLinkCombo = createComboBox();
        DefaultComboBoxModel model = (DefaultComboBoxModel) webLinkCombo.getModel();
        model.addElement(webLinkDefNone);
        webLinkCombo.setSelectedIndex(0);

        inner.add(webLinkCombo, cc.xy(1, 1));
        inner.add(webLinkMoreBtn, cc.xy(3, 1));

        ActionListener wlchanged = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                formHasChanged();

                boolean hasWL = webLinkCombo.getSelectedIndex() > 0;
                webLinkCombo.setEnabled(hasWL);
                if (hasWL) {
                    formatCombo.setSelectedIndex(formatCombo.getModel().getSize() > 0 ? 0 : -1);
                    pickListCBX.setSelectedIndex(pickListCBX.getModel().getSize() > 0 ? 0 : -1);
                }
            }
        };
        webLinkCombo.addActionListener(wlchanged);

        formatterPanel.add(SL_WEBLINK, inner.getPanel());
    }

    //--------------------------
    // PickList
    //--------------------------

    pickListCBX = createComboBox(new DefaultComboBoxModel());
    pickListCBX.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            formHasChanged();
            if (formatCombo != null && pickListCBX.getSelectedIndex() > 0) {
                formatCombo.setSelectedIndex(formatCombo.getModel().getSize() > 0 ? 0 : -1);
                webLinkCombo.setSelectedIndex(webLinkCombo.getModel().getSize() > 0 ? 0 : -1);
            }
        }
    });
    pickListMoreBtn = createButton(ELIPSES);
    pickListMoreBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            LocalizableItemIFace currentField = prevField;
            prevField = getSelectedFieldItem();

            PickList selectedItem = (PickList) pickListCBX.getSelectedItem();

            PickListEditorDlg dlg = new PickListEditorDlg(
                    localizableIO.hasUpdatablePickLists() ? null : localizableIO, true, true);
            dlg.setTableInfo(tableInfo);
            dlg.setFieldInfo(fieldInfo);
            dlg.createUI();
            dlg.setSize(400, 500);
            dlg.setVisible(true);
            if (!dlg.isCancelled()) {
                if (dlg.hasChanged()) {
                    hasChanged = true;
                    Vector<PickList> list = dlg.getNewPickLists();
                    DefaultComboBoxModel plCbxModel = (DefaultComboBoxModel) pickListCBX.getModel();
                    for (int i = 0; i < plCbxModel.getSize(); i++) {
                        list.add((PickList) plCbxModel.getElementAt(i));
                    }
                    Collections.sort(list);
                    plCbxModel.removeAllElements();
                    plCbxModel.addElement(pickListNone);
                    int inx = -1;
                    int i = 0;
                    for (PickList pl : list) {
                        plCbxModel.addElement(pl);
                        if (inx == -1 && selectedItem != null
                                && ((selectedItem.getId() != null && pl.getId() != null
                                        && selectedItem.getId().equals(pl.getId()))
                                        || (selectedItem.getName() != null && pl.getName() != null
                                                && selectedItem.getName().equals(pl.getName())))) {
                            inx = i;
                        }
                        i++;
                    }
                    pickListCBX.setSelectedIndex(inx + 1);
                }
            }

            prevField = currentField;
        }
    });

    if (includeFormatAndAutoNumUI) {
        PanelBuilder inner = new PanelBuilder(new FormLayout("max(p;150px),2px,min", "p"));
        inner.add(pickListCBX, cc.xy(1, 1));
        inner.add(pickListMoreBtn, cc.xy(3, 1));

        formatterPanel.add(SL_PICKLIST, inner.getPanel());

    } else {
        pb.add(pickListLbl = createI18NFormLabel(SL_PICKLIST, SwingConstants.RIGHT), cc.xy(3, y));
        pb.add(pickListCBX, cc.xy(5, y));
        pb.add(pickListMoreBtn, cc.xy(7, y));
        y += 2;
    }

    nxtBtn = createI18NButton("SL_NEXT");
    nxtEmptyBtn = createI18NButton("SL_NEXT_EMPTY");
    fldSpellChkBtn = createI18NButton("SL_SPELL_CHECK");

    //JPanel bbp = ButtonBarFactory.buildCenteredBar(adjustButtonArray(new JButton[] {nxtEmptyBtn, nxtBtn, fldSpellChkBtn}));
    //bbp.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
    //pb.add(bbp,   cc.xywh(3, y, 8, 1));

    nxtBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            next();
        }
    });
    nxtEmptyBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            nextEmpty();
        }
    });

    fldSpellChkBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (checker != null) {
                checker.spellCheck(fieldDescText);
                checker.spellCheck(fieldNameText);
            }
        }

    });

    fieldHideChk.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            formHasChanged();
        }

    });

    fieldReqChk.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            formHasChanged();
        }

    });

    DocumentListener dl = new DocumentAdaptor() {
        @Override
        protected void changed(DocumentEvent e) {
            formHasChanged();
        }
    };

    fieldNameText.getDocument().addDocumentListener(dl);
    fieldDescText.getDocument().addDocumentListener(dl);
    /*        
            if (formatTxt != null)
            {
    formatTxt.getDocument().addDocumentListener(dl);
            }
    */
    SchemaI18NService.getInstance().checkCurrentLocaleMenu();

    enableUIControls(false);

    setIgnoreChanges(false);
}