Java JComboBox Selection getTypedSelectedItemFromCombo(JComboBox combo)

Here you can find the source of getTypedSelectedItemFromCombo(JComboBox combo)

Description

Since JComboBox.getSelectedItem() returns a plain Object, this allows us to get the appropriate type of object instead.

License

Open Source License

Declaration

public static <E> E getTypedSelectedItemFromCombo(JComboBox<E> combo) 

Method Source Code

//package com.java2s;
/*//from ww w . ja  va2 s  .c om
 * Copyright (c) 1998-2017 by Richard A. Wilkes. All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, version 2.0. If a copy of the MPL was not distributed with
 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, version 2.0.
 */

import javax.swing.JComboBox;

public class Main {
    /**
     * 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 ? combo.getItemAt(index) : null;
    }
}

Related

  1. anySelected(JComboBox box)
  2. getSelectedItem(Object combo)
  3. getSelectedName(JComboBox comboBox)
  4. getSelectedString(JComboBox jcb)
  5. safeSelectFirst(JComboBox cb)
  6. selectComboBoxItem(JComboBox jComboBox, String name)
  7. selectStringInList(String string, JComboBox list)
  8. selectWithoutNotifyingListeners(JComboBox comboBox, int selectedIndex)