Java JComboBox Value createComboBox(final Preferences node, final String pref_name, final String[] options, final String default_value)

Here you can find the source of createComboBox(final Preferences node, final String pref_name, final String[] options, final String default_value)

Description

create Combo Box

License

Open Source License

Declaration

public static JComboBox createComboBox(final Preferences node, final String pref_name, final String[] options,
            final String default_value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.swing.JComboBox;
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.prefs.Preferences;

public class Main {
    public static JComboBox createComboBox(final Preferences node, final String pref_name, final String[] options,
            final String default_value) {
        final String[] interned_options = new String[options.length];
        for (int i = 0; i < options.length; ++i) {
            interned_options[i] = options[i].intern();
        }/*from   ww w.  j a va  2s. c o m*/
        default_value.intern();
        final JComboBox combo_box = new JComboBox(interned_options);
        final String current_stored_value = node.get(pref_name, default_value).intern();
        combo_box.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent ae) {
                final String selection = (String) combo_box.getSelectedItem();
                if (selection != null) {
                    node.put(pref_name, selection);
                }
            }
        });
        combo_box.setSelectedItem(current_stored_value);
        node.addPreferenceChangeListener(new PreferenceChangeListener() {
            @Override
            public void preferenceChange(final PreferenceChangeEvent evt) {
                if (evt.getNode().equals(node) && evt.getKey().equals(pref_name)
                        && !combo_box.getSelectedItem().equals(evt.getNewValue()) && evt.getNewValue() != null) {
                    combo_box.setSelectedItem(evt.getNewValue().intern());
                }
            }
        });
        return combo_box;
    }
}

Related

  1. containsValue(JComboBox comboBox, String value)
  2. createComboBox(ActionListener actionListener, Vector values, String command)
  3. createStandardCombo(String[] values)
  4. fillValue(Vector vtData, int iComboIndex, int iVectorIndex, JComboBox cbo, Vector vt, boolean bClear, boolean bHaveNull)
  5. getCmbValue(JComboBox cmb)
  6. getDouble(javax.swing.JComboBox input)