Example usage for javax.swing DefaultComboBoxModel getSelectedItem

List of usage examples for javax.swing DefaultComboBoxModel getSelectedItem

Introduction

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

Prototype

public Object getSelectedItem() 

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);/*  w w  w.ja v  a  2s. 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.getSelectedItem());
        }
    };
    button.addActionListener(actionListener);

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

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

private void projComboBoxActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_projComboBoxActionPerformed
{//GEN-HEADEREND:event_projComboBoxActionPerformed

    final DefaultComboBoxModel dcm = (DefaultComboBoxModel) projComboBox.getModel();

    final Projector p = (Projector) dcm.getSelectedItem();

    params.getProjectionParameters().setProjectionName(p.toString());

    final String path = projManager.formatThumbnailName(p.getThumbnailName());

    final URL imgURL = projManager.getClass().getResource(path);

    if (imgURL != null) {
        final ImageIcon i = new ImageIcon(imgURL, "Projection Preview Image");

        previewLabel.setText("");
        previewLabel.setIcon(i);//  www .j  a  v  a2 s.  com
    } else {
        previewLabel.setText("Preview Icon Not Found!\n" + path);
        previewLabel.setIcon(null);
    }
}

From source file:op.care.supervisor.PnlHandover.java

private List<Component> addFilters() {
    List<Component> list = new ArrayList<Component>();

    Pair<DateTime, DateTime> minmax = NReportTools.getMinMax();
    if (minmax != null) {
        final DefaultComboBoxModel yearModel = new DefaultComboBoxModel();
        for (int year = new LocalDate().getYear(); year >= minmax.getFirst().getYear(); year--) {
            yearModel.addElement(year);//from ww w  .ja va  2s.  co  m
        }

        JPanel innerPanel = new JPanel();
        innerPanel.setOpaque(false);
        innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.PAGE_AXIS));
        txtSearch = new JXSearchField(SYSTools.xx("misc.msg.searchphrase"));
        txtSearch.setInstantSearchDelay(100000);
        txtSearch.setFont(SYSConst.ARIAL14);
        txtSearch.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (SYSTools.catchNull(txtSearch.getText()).trim().length() > 3) {
                    SYSFilesTools.print(NReportTools.getReportsAndHandoversAsHTML(
                            NReportTools.getNReports4Handover((Homes) cmbHomes.getSelectedItem(),
                                    txtSearch.getText().trim(),
                                    Integer.parseInt(yearModel.getSelectedItem().toString())),
                            txtSearch.getText().trim(),
                            Integer.parseInt(yearModel.getSelectedItem().toString())), false);
                }
            }
        });
        innerPanel.add(txtSearch);
        JButton btnSearchGeneralReports = GUITools
                .createHyperlinkButton("nursingrecords.handover.searchHandovers", null, null);
        btnSearchGeneralReports.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List listHandovers = HandoversTools.getBy(
                        Integer.parseInt(yearModel.getSelectedItem().toString()),
                        (Homes) cmbHomes.getSelectedItem());
                SYSFilesTools.print(NReportTools.getReportsAndHandoversAsHTML(listHandovers, "",
                        Integer.parseInt(yearModel.getSelectedItem().toString())), false);
            }
        });
        innerPanel.add(btnSearchGeneralReports);
        yearCombo = new JXComboBox(yearModel);
        yearCombo.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                txtSearch.postActionEvent();
            }
        });

        JPanel myPanel = new JPanel();
        myPanel.setOpaque(false);
        myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.LINE_AXIS));
        myPanel.add(innerPanel);
        myPanel.add(yearCombo);
        list.add(myPanel);
    }

    cmbHomes = new JComboBox();
    cmbHomes.setFont(SYSConst.ARIAL14);
    HomesTools.setComboBox(cmbHomes);
    cmbHomes.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent itemEvent) {
            if (itemEvent.getStateChange() != ItemEvent.SELECTED)
                return;
            reloadDisplay();
        }
    });
    list.add(cmbHomes);

    tbResidentFirst = GUITools.getNiceToggleButton("nursingrecords.handover.residentFirst");
    SYSPropsTools.restoreState("nursingrecords.handover.tbResidentFirst", tbResidentFirst);
    tbResidentFirst.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            SYSPropsTools.storeState("nursingrecords.handover.tbResidentFirst", tbResidentFirst);
            reload();
        }
    });
    tbResidentFirst.setHorizontalAlignment(SwingConstants.LEFT);
    list.add(tbResidentFirst);

    return list;
}

From source file:org.mbs3.juniuploader.gui.pnlFormVariables.java

private void initGUI() {
    try {//from w  w w .java 2  s.c o  m
        GridBagLayout thisLayout = new GridBagLayout();
        this.setLayout(thisLayout);
        {
            btnAddPair = new JButton();
            this.add(btnAddPair, new GridBagConstraints(3, 3, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
            btnAddPair.setText("Add a New Pair");
            btnAddPair.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    btnAddPairActionPerformed(evt);
                }
            });
        }
        {
            btnRemPair = new JButton();
            this.add(btnRemPair, new GridBagConstraints(3, 6, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
            btnRemPair.setText("Remove Selected Pair");
            btnRemPair.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    btnRemPairActionPerformed(evt);
                }
            });
        }
        {

            cmbFormGroups = new JComboBox();
            this.add(cmbFormGroups, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
            cmbFormGroups.setModel(frmMain.formGroups);
            cmbFormGroups.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent evt) {
                    cmbFormGroupsItemStateChanged(evt);
                }
            });
        }
        {

            DefaultComboBoxModel groups = frmMain.formGroups;
            FormPairGroup fpg = (FormPairGroup) groups.getSelectedItem();
            DefaultComboBoxModel dcbmFormPairs;
            if (fpg != null)
                dcbmFormPairs = new DefaultComboBoxModel(fpg.getPairs());
            else
                dcbmFormPairs = new DefaultComboBoxModel();

            lstFormPairValues = new JList();
            lstFormPairValues.setModel(dcbmFormPairs);

            this.add(lstFormPairValues, new GridBagConstraints(1, 3, 1, 4, 0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
            lstFormPairValues.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
        }

        {
            btnAddGroup = new JButton();
            this.add(btnAddGroup, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            btnAddGroup.setText("Add a Group");
            btnAddGroup.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    btnAddGroupActionPerformed(evt);
                }
            });
        }
        {
            btnRemSelGrp = new JButton();
            this.add(btnRemSelGrp, new GridBagConstraints(5, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            btnRemSelGrp.setText("Remove This Group");
            btnRemSelGrp.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    btnRemSelGrpActionPerformed(evt);
                }
            });
        }
        {
            btnEditPair = new JButton();
            this.add(btnEditPair, new GridBagConstraints(3, 4, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                    GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
            btnEditPair.setText("Edit This Pair");
            btnEditPair.setEnabled(false);
        }
        thisLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.1, 0.1, 0.0, 0.1, 0.0 };
        thisLayout.rowHeights = new int[] { 8, 24, 9, 20, 7, 8, 7, 9 };
        thisLayout.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
        thisLayout.columnWidths = new int[] { 11, 335, 9, 97, 10, 119, 10 };
        this.setPreferredSize(new java.awt.Dimension(614, 382));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.pentaho.ui.xul.swing.tags.SwingMenuList.java

public void layout() {
    inLayoutProcess = true;//from   w  w  w. j a v a 2s .c o  m
    if (suppressLayout) {
        inLayoutProcess = false;
        return;
    }
    SwingMenupopup popup = getPopupElement();
    DefaultComboBoxModel model = (DefaultComboBoxModel) this.combobox.getModel();
    model.removeAllElements();

    SwingMenuitem selectedItem = null;

    // capture first child as default selection
    boolean firstChild = true;
    for (XulComponent item : popup.getChildNodes()) {
        JMenuItem jmenuItem = (JMenuItem) ((SwingMenuitem) item).getManagedObject();
        SwingMenuitem tempItem = (SwingMenuitem) item;
        model.addElement(tempItem);

        if (tempItem.isSelected() || firstChild) {
            selectedItem = tempItem;
            firstChild = false;
        }
    }

    inLayoutProcess = false;

    if (selectedItem != null) {
        // if first setting it to the currently selected one will not fire event.
        // manually firing here
        if (model.getSelectedItem() == selectedItem) {
            fireSelectedEvents();
        }
        model.setSelectedItem(selectedItem);
    }
    initialized = true;
    loaded = true;
}

From source file:pipeline.GUI_utils.ListOfPointsView.java

public String getFieldForColoring() {
    @SuppressWarnings("rawtypes")
    DefaultComboBoxModel comboBoxModel = coloringComboBox == null ? null
            : (DefaultComboBoxModel) coloringComboBox.getModel();
    if (comboBoxModel != null) {
        if (comboBoxModel.getSelectedItem() == null)
            return null;
        else/* w w  w. jav  a 2s . c o m*/
            return comboBoxModel.getSelectedItem().toString();
    } else
        return null;
}