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

/**
 * Get double from the JComboBox input/*from w ww. ja  v a2s . c o  m*/
 * 
 * @param input the JComboBox object
 * @return the double number
 */
public static double getDouble(javax.swing.JComboBox input) throws Exception {
    return new Double((String) (input.getSelectedItem())).doubleValue();
}

From source file:Main.java

/**
 * Opens a file chooser dialog//from w  w  w.  ja v a2s  .c o m
 * @param chooseAFolder true if the dialog should return a folder, false for a file
 */
public static String chooseFile(JComboBox comboBox, boolean chooseAFolder) {
    JFileChooser chooser = new JFileChooser();
    String path = "";
    String currentDir = "";

    if (comboBox.getSelectedItem() != null) {
        currentDir = comboBox.getSelectedItem().toString();
    }

    // Set whether we want to select a folder instead of a file
    if (chooseAFolder) {
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    }

    chooser.setCurrentDirectory(new File(currentDir));

    // Show open dialog; this method does not return until the dialog is closed
    if (chooser.showOpenDialog(comboBox) == JFileChooser.APPROVE_OPTION) {
        path = chooser.getSelectedFile().getAbsolutePath();
    }

    if (path == null || path.equals("")) {
        return currentDir;
    }
    return path;
}

From source file:org.jdal.swing.form.FormUtils.java

/**
 * Add a link on primary and dependent JComboBoxes by property name. 
 * When selection changes on primary use propertyName to get a Collection and fill dependent JComboBox with it
 * @param primary JComboBox when selection changes
 * @param dependent JComboBox that are filled with collection   
 * @param propertyName the property name for get the collection from primary selected item
 * @param addNull if true, add a null as first combobox item
 *//* w w  w . j av  a  2s  . c  om*/
@SuppressWarnings("rawtypes")
public static void link(final JComboBox primary, final JComboBox dependent, final String propertyName,
        final boolean addNull) {

    primary.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Object selected = primary.getSelectedItem();
            if (selected != null) {
                BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(selected);
                if (wrapper.isWritableProperty(propertyName)) {
                    Collection collection = (Collection) wrapper.getPropertyValue(propertyName);
                    Vector<Object> vector = new Vector<Object>(collection);
                    if (addNull)
                        vector.add(0, null);
                    DefaultComboBoxModel model = new DefaultComboBoxModel(vector);
                    dependent.setModel(model);
                } else {
                    log.error("Can't write on propety '" + propertyName + "' of class: '" + selected.getClass()
                            + "'");
                }
            }
        }
    });
}

From source file:Main.java

public void actionPerformed(ActionEvent evt) {
    JComboBox cb = (JComboBox) evt.getSource();
    Object newItem = cb.getSelectedItem();

    boolean same = newItem.equals(oldItem);
    oldItem = newItem;// w  ww.  j  a  v a 2 s  . c o m

    if ("comboBoxEdited".equals(evt.getActionCommand())) {
        // User has typed in a string; only possible with an editable combobox
    } else if ("comboBoxChanged".equals(evt.getActionCommand())) {
        // User has selected an item; it may be the same item
    }
}

From source file:FontComboBox.java

public void actionPerformed(ActionEvent evt) {
    JComboBox source = (JComboBox) evt.getSource();
    String item = (String) source.getSelectedItem();
    fontLabel.setFont(new Font(item, Font.PLAIN, 12));
}

From source file:Main.java

public Main() {
    String[] comboTypes = { "Numbers", "Alphabets", "Symbols" };
    JComboBox<String> comboTypesList = new JComboBox<>(comboTypes);
    comboTypesList.setSelectedIndex(2);//from w  w  w.  j  a  v  a 2  s .c  om
    comboTypesList.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JComboBox jcmbType = (JComboBox) e.getSource();
            String cmbType = (String) jcmbType.getSelectedItem();
            System.out.println(cmbType);
        }
    });
    setLayout(new BorderLayout());
    add(comboTypesList, BorderLayout.NORTH);
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    JComboBox comboBox = (JComboBox) e.getSource();
    Item item = (Item) comboBox.getSelectedItem();
    System.out.println(item.getId() + " : " + item.getDescription());
}

From source file:Main.java

public Main() {
    JComboBox comboBox = new JComboBox();
    comboBox.addItem(new Item(1, "-"));
    comboBox.addItem(new Item(2, "X"));
    comboBox.addItem(new Item(3, "Y"));
    comboBox.setMaximumRowCount(3);//from   w w  w .  j  a v a 2  s  .  co m
    comboBox.setPrototypeDisplayValue(" None of the above ");
    comboBox.addActionListener(e -> {
        JComboBox c = (JComboBox) e.getSource();
        Item item = (Item) c.getSelectedItem();
        System.out.println(item.getId() + " : " + item.getDescription());
    });
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(comboBox);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox) e.getSource();
    CheckComboStore store = (CheckComboStore) cb.getSelectedItem();
    CheckComboRenderer ccr = (CheckComboRenderer) cb.getRenderer();
    ccr.checkBox.setSelected((store.state = !store.state));
}

From source file:SelectionHandler.java

public void actionPerformed(ActionEvent a) {
    JComboBox cb = (JComboBox) a.getSource();
    String selected = (String) cb.getSelectedItem();
    String current = direction.getText();
    if (!selected.equals(current)) {
        direction.setText(selected);//  www  .j  av  a 2s.  c  o m
        JList temp = source;
        source = destination;
        destination = temp;
        source.clearSelection();
        destination.clearSelection();
    }

}