Example usage for javax.swing JComboBox getSelectedItem

List of usage examples for javax.swing JComboBox getSelectedItem

Introduction

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

Prototype

public Object getSelectedItem() 

Source Link

Document

Returns the current selected item.

Usage

From source file:Main.java

private JComboBox createComboBox(final Map<Integer, Student> map) {
    final JComboBox cbox = new JComboBox();
    for (Integer id : map.keySet()) {
        cbox.addItem(id);/*from  ww w .  j a  v a 2  s  .  co  m*/
    }

    cbox.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Integer id = (Integer) cbox.getSelectedItem();
            System.out.println(map.get(id));
        }
    });

    return cbox;
}

From source file:org.kineticsystem.commons.data.demo.panels.AggregationChartPane.java

/**
 * Constructor.// w ww  . j a  v a  2 s . c o  m
 * @param source This is the source list being modified ate the same time
 *     by many threads.
 */
public AggregationChartPane(ActiveList<RandomContact> source) {

    // Define aggregators.

    GroupAggregator[] aggregators = new GroupAggregator[] { new ContactsPerContinentAggr(),
            new AvgAgePerContinentAggr(), new MaxAgePerContinentAggr(), new MinAgePerContinentAggr() };

    // Aggregator selector.

    DefaultComboBoxModel groupComboModel = new DefaultComboBoxModel(aggregators);
    final JComboBox groupCombo = new JComboBox(groupComboModel);
    groupCombo.setSelectedIndex(1);
    groupCombo.setToolTipText("Select an aggregation function.");

    // Create the dataset.

    dataset = new DefaultCategoryDataset();
    List<Country> countries = RandomContactGenerator.getCountries();
    Set<String> continents = new TreeSet<String>();

    for (Country country : countries) {
        continents.add(country.getContinent());
    }

    for (String continent : continents) {
        dataset.setValue(0, continent, "");
    }

    // Define the aggregated list.

    groups = new GroupMapping<RandomContact>(source);
    groups.setAggregator(aggregators[0]);
    groups.getTarget().addActiveListListener(this);

    // Create the chart.

    JFreeChart chart = ChartFactory.createBarChart("", "Continent", groups.getAggregator().toString(), dataset,
            PlotOrientation.VERTICAL, true, // legend?
            true, // tooltips?
            false // URLs?
    );
    final ValueAxis axis = chart.getCategoryPlot().getRangeAxis();
    axis.setAutoRange(true);
    axis.setRange(0, 250);

    ChartPanel chartPanel = new ChartPanel(chart);

    // Create the selector.

    ActionListener groupComboListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox) e.getSource();
            GroupAggregator aggr = (GroupAggregator) cb.getSelectedItem();
            groups.setAggregator(aggr);
            axis.setLabel(aggr.toString());
        }
    };
    groupCombo.addActionListener(groupComboListener);

    // Layout the GUI.

    Cell cell = new Cell();

    TetrisLayout groupTableLayout = new TetrisLayout(2, 1);
    groupTableLayout.setRowWeight(0, 0);
    groupTableLayout.setRowWeight(1, 100);

    setLayout(groupTableLayout);
    add(groupCombo, cell);
    add(chartPanel, cell);
}

From source file:hermes.browser.dialog.ClasspathIdCellEdtitor.java

public Component getTableCellEditorComponent(JTable table, Object value, boolean arg2, int arg3, int arg4) {
    try {/*w w w .j  av  a 2  s .  com*/
        if (tableToCombo.containsKey(table)) {
            return (Component) tableToCombo.get(table);
        } else {
            final Vector items = new Vector();

            for (Iterator iter = HermesBrowser.getBrowser().getConfig().getClasspathGroup().iterator(); iter
                    .hasNext();) {
                ClasspathGroupConfig config = (ClasspathGroupConfig) iter.next();

                items.add(config.getId());
            }

            items.add("System");

            final JComboBox combo = new JComboBox(items);

            combo.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    selection = (String) combo.getSelectedItem();
                }
            });

            if (value != null) {
                combo.setSelectedItem(value);
            }

            log.debug("value=" + value);

            tableToCombo.put(table, combo);
            return combo;
        }
    } catch (HermesException e) {
        log.error(e.getMessage(), e);
    }

    return table;
}

From source file:cool.pandora.modeller.ui.BagTableFormBuilder.java

/**
 * addList.//  w w w .  jav a2 s .c  o  m
 *
 * @param isRequired   boolean
 * @param label        String
 * @param elements     Collection
 * @param defaultValue String
 * @param checkbox     JComponent
 * @return addBinding
 */
public JComponent[] addList(final boolean isRequired, final String label, final Collection<String> elements,
        final String defaultValue, final JComponent checkbox) {
    final ArrayList<String> listModel = new ArrayList<>();
    listModel.addAll(elements);

    // Set default value selected from value list
    final JComboBox<String> dropDownTextField = new JComboBox<>(
            listModel.toArray(new String[listModel.size()]));
    dropDownTextField.setSelectedItem(defaultValue);
    final Object obj = dropDownTextField.getSelectedItem();
    dropDownTextField.setSelectedItem(obj);

    return addBinding(isRequired, label, dropDownTextField, checkbox);
}

From source file:cool.pandora.modeller.ui.jpanel.base.BagInfoForm.java

/**
 * getBagInfoMap.//ww w. ja v  a2 s  .co  m
 *
 * @return map
 */
public HashMap<String, String> getBagInfoMap() {
    final HashMap<String, String> map = new HashMap<>();
    String key = "";
    String value = "";
    final Component[] components = getFieldComponents();
    for (int i = 0; i < components.length; i++) {
        Component c;
        c = components[i];
        if (c instanceof JLabel) {
            final JLabel label = (JLabel) c;
            key = label.getText();
        }
        i++;
        // Is required component
        c = components[i];
        i++;
        c = components[i];
        if (c instanceof JTextField) {
            final JTextField tf = (JTextField) c;
            value = tf.getText();
        } else if (c instanceof JTextArea) {
            final JTextArea ta = (JTextArea) c;
            value = ta.getText();
        } else if (c instanceof JComboBox) {
            final JComboBox<?> tb = (JComboBox<?>) c;
            value = (String) tb.getSelectedItem();
        }
        map.put(key, value);
        i++;
        c = components[i];
    }
    return map;
}

From source file:de.codesourcery.eve.skills.ui.components.impl.planning.CalendarEntryEditorComponent.java

private Duration toDuration(JTextField textField, JComboBox box) {
    final Duration.Type type = (Duration.Type) box.getSelectedItem();
    final long value = Long.parseLong(textField.getText());
    return new Duration(type.toSeconds() * value);
}

From source file:events.ListSelectionDemo.java

public ListSelectionDemo() {
    super(new BorderLayout());

    String[] listData = { "one", "two", "three", "four", "five", "six", "seven" };
    String[] columnNames = { "French", "Spanish", "Italian" };
    list = new JList(listData);

    listSelectionModel = list.getSelectionModel();
    listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
    JScrollPane listPane = new JScrollPane(list);

    JPanel controlPane = new JPanel();
    String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" };

    final JComboBox comboBox = new JComboBox(modes);
    comboBox.setSelectedIndex(2);//from   w w w  .  j a v  a  2s  .  c o  m
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String newMode = (String) comboBox.getSelectedItem();
            if (newMode.equals("SINGLE_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            } else {
                listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            }
            output.append("----------" + "Mode: " + newMode + "----------" + newline);
        }
    });
    controlPane.add(new JLabel("Selection mode:"));
    controlPane.add(comboBox);

    //Build output area.
    output = new JTextArea(1, 10);
    output.setEditable(false);
    JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    //Do the layout.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    add(splitPane, BorderLayout.CENTER);

    JPanel topHalf = new JPanel();
    topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS));
    JPanel listContainer = new JPanel(new GridLayout(1, 1));
    listContainer.setBorder(BorderFactory.createTitledBorder("List"));
    listContainer.add(listPane);

    topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
    topHalf.add(listContainer);
    //topHalf.add(tableContainer);

    topHalf.setMinimumSize(new Dimension(100, 50));
    topHalf.setPreferredSize(new Dimension(100, 110));
    splitPane.add(topHalf);

    JPanel bottomHalf = new JPanel(new BorderLayout());
    bottomHalf.add(controlPane, BorderLayout.PAGE_START);
    bottomHalf.add(outputPane, BorderLayout.CENTER);
    //XXX: next line needed if bottomHalf is a scroll pane:
    //bottomHalf.setMinimumSize(new Dimension(400, 50));
    bottomHalf.setPreferredSize(new Dimension(450, 135));
    splitPane.add(bottomHalf);
}

From source file:events.TableListSelectionDemo.java

public TableListSelectionDemo() {
    super(new BorderLayout());

    String[] columnNames = { "French", "Spanish", "Italian" };
    String[][] tableData = { { "un", "uno", "uno" }, { "deux", "dos", "due" }, { "trois", "tres", "tre" },
            { "quatre", "cuatro", "quattro" }, { "cinq", "cinco", "cinque" }, { "six", "seis", "sei" },
            { "sept", "siete", "sette" } };

    table = new JTable(tableData, columnNames);
    listSelectionModel = table.getSelectionModel();
    listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
    table.setSelectionModel(listSelectionModel);
    JScrollPane tablePane = new JScrollPane(table);

    //Build control area (use default FlowLayout).
    JPanel controlPane = new JPanel();
    String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" };

    final JComboBox comboBox = new JComboBox(modes);
    comboBox.setSelectedIndex(2);//  ww  w.  j  a v a  2  s . c  o m
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String newMode = (String) comboBox.getSelectedItem();
            if (newMode.equals("SINGLE_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            } else {
                listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            }
            output.append("----------" + "Mode: " + newMode + "----------" + newline);
        }
    });
    controlPane.add(new JLabel("Selection mode:"));
    controlPane.add(comboBox);

    //Build output area.
    output = new JTextArea(1, 10);
    output.setEditable(false);
    JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    //Do the layout.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    add(splitPane, BorderLayout.CENTER);

    JPanel topHalf = new JPanel();
    topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS));
    JPanel listContainer = new JPanel(new GridLayout(1, 1));
    JPanel tableContainer = new JPanel(new GridLayout(1, 1));
    tableContainer.setBorder(BorderFactory.createTitledBorder("Table"));
    tableContainer.add(tablePane);
    tablePane.setPreferredSize(new Dimension(420, 130));
    topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
    topHalf.add(listContainer);
    topHalf.add(tableContainer);

    topHalf.setMinimumSize(new Dimension(250, 50));
    topHalf.setPreferredSize(new Dimension(200, 110));
    splitPane.add(topHalf);

    JPanel bottomHalf = new JPanel(new BorderLayout());
    bottomHalf.add(controlPane, BorderLayout.PAGE_START);
    bottomHalf.add(outputPane, BorderLayout.CENTER);
    //XXX: next line needed if bottomHalf is a scroll pane:
    //bottomHalf.setMinimumSize(new Dimension(400, 50));
    bottomHalf.setPreferredSize(new Dimension(450, 110));
    splitPane.add(bottomHalf);
}

From source file:ListSelectionDemo.java

public ListSelectionDemo() {
    super(new BorderLayout());

    String[] listData = { "one", "two", "three", "four", "five", "six", "seven" };
    String[] columnNames = { "French", "Spanish", "Italian" };
    list = new JList(listData);

    listSelectionModel = list.getSelectionModel();
    listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
    JScrollPane listPane = new JScrollPane(list);

    JPanel controlPane = new JPanel();
    String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" };

    final JComboBox comboBox = new JComboBox(modes);
    comboBox.setSelectedIndex(2);//from ww  w.j  a  va 2 s.com
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String newMode = (String) comboBox.getSelectedItem();
            if (newMode.equals("SINGLE_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            } else {
                listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            }
            output.append("----------" + "Mode: " + newMode + "----------" + newline);
        }
    });
    controlPane.add(new JLabel("Selection mode:"));
    controlPane.add(comboBox);

    // Build output area.
    output = new JTextArea(1, 10);
    output.setEditable(false);
    JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    // Do the layout.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    add(splitPane, BorderLayout.CENTER);

    JPanel topHalf = new JPanel();
    topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS));
    JPanel listContainer = new JPanel(new GridLayout(1, 1));
    listContainer.setBorder(BorderFactory.createTitledBorder("List"));
    listContainer.add(listPane);

    topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
    topHalf.add(listContainer);
    // topHalf.add(tableContainer);

    topHalf.setMinimumSize(new Dimension(100, 50));
    topHalf.setPreferredSize(new Dimension(100, 110));
    splitPane.add(topHalf);

    JPanel bottomHalf = new JPanel(new BorderLayout());
    bottomHalf.add(controlPane, BorderLayout.PAGE_START);
    bottomHalf.add(outputPane, BorderLayout.CENTER);
    // XXX: next line needed if bottomHalf is a scroll pane:
    // bottomHalf.setMinimumSize(new Dimension(400, 50));
    bottomHalf.setPreferredSize(new Dimension(450, 135));
    splitPane.add(bottomHalf);
}

From source file:TableSelectionDemo.java

public TableSelectionDemo() {
    super(new BorderLayout());

    String[] columnNames = { "French", "Spanish", "Italian" };
    String[][] tableData = { { "un", "uno", "uno" }, { "deux", "dos", "due" }, { "trois", "tres", "tre" },
            { "quatre", "cuatro", "quattro" }, { "cinq", "cinco", "cinque" }, { "six", "seis", "sei" },
            { "sept", "siete", "sette" } };

    table = new JTable(tableData, columnNames);
    listSelectionModel = table.getSelectionModel();
    listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
    table.setSelectionModel(listSelectionModel);
    JScrollPane tablePane = new JScrollPane(table);

    // Build control area (use default FlowLayout).
    JPanel controlPane = new JPanel();
    String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" };

    final JComboBox comboBox = new JComboBox(modes);
    comboBox.setSelectedIndex(2);/*from  w ww  .  ja va2s. com*/
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String newMode = (String) comboBox.getSelectedItem();
            if (newMode.equals("SINGLE_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            } else {
                listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            }
            output.append("----------" + "Mode: " + newMode + "----------" + newline);
        }
    });
    controlPane.add(new JLabel("Selection mode:"));
    controlPane.add(comboBox);

    // Build output area.
    output = new JTextArea(1, 10);
    output.setEditable(false);
    JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    // Do the layout.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    add(splitPane, BorderLayout.CENTER);

    JPanel topHalf = new JPanel();
    topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS));
    JPanel listContainer = new JPanel(new GridLayout(1, 1));
    JPanel tableContainer = new JPanel(new GridLayout(1, 1));
    tableContainer.setBorder(BorderFactory.createTitledBorder("Table"));
    tableContainer.add(tablePane);
    tablePane.setPreferredSize(new Dimension(420, 130));
    topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
    topHalf.add(listContainer);
    topHalf.add(tableContainer);

    topHalf.setMinimumSize(new Dimension(250, 50));
    topHalf.setPreferredSize(new Dimension(200, 110));
    splitPane.add(topHalf);

    JPanel bottomHalf = new JPanel(new BorderLayout());
    bottomHalf.add(controlPane, BorderLayout.PAGE_START);
    bottomHalf.add(outputPane, BorderLayout.CENTER);
    // XXX: next line needed if bottomHalf is a scroll pane:
    // bottomHalf.setMinimumSize(new Dimension(400, 50));
    bottomHalf.setPreferredSize(new Dimension(450, 110));
    splitPane.add(bottomHalf);
}