Java JComboBox Value savePrefs(Preferences prefs, String prefKey, JComboBox combo, String newValidValue)

Here you can find the source of savePrefs(Preferences prefs, String prefKey, JComboBox combo, String newValidValue)

Description

save Prefs

License

Open Source License

Declaration

public static void savePrefs(Preferences prefs, String prefKey, JComboBox combo, String newValidValue) 

Method Source Code

//package com.java2s;
/**********************************************************************************************
 *
 * Asprise OCR Java API/*from w  w  w. j  a  va 2s. co m*/
 * Copyright (C) 1998-2015. Asprise Inc. <asprise.com>
 *
 * This file is licensed under the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation.
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * You should have received a copy of the GNU Affero General Public License.  If not, please
 * visit <http://www.gnu.org/licenses/agpl-3.0.html>.
 *
 **********************************************************************************************/

import javax.swing.DefaultComboBoxModel;

import javax.swing.JComboBox;

import java.util.prefs.Preferences;

public class Main {
    static final String DELIMITER = "`";

    public static void savePrefs(Preferences prefs, String prefKey, JComboBox combo, String newValidValue) {
        if (newValidValue == null) {
            return;
        }

        DefaultComboBoxModel comboModel = (DefaultComboBoxModel) combo.getModel();

        int existingIndex = comboModel.getIndexOf(newValidValue);
        if (existingIndex >= 0) {
            comboModel.removeElementAt(existingIndex);
        }
        comboModel.insertElementAt(newValidValue, 0);
        combo.setSelectedIndex(0);

        StringBuilder entries = new StringBuilder();
        int size = Math.min(comboModel.getSize(), 20);
        for (int i = 0; i < size; i++) {
            entries.append(comboModel.getElementAt(i));
            if (i != size - 1) {
                entries.append(DELIMITER);
            }
        }

        while (entries.length() > Preferences.MAX_VALUE_LENGTH) {
            int lastIndex = entries.lastIndexOf(DELIMITER);
            if (lastIndex == -1) {
                break;
            } else {
                entries.delete(lastIndex, entries.length());
            }
        }

        prefs.put(prefKey, entries.toString());
        try {
            prefs.flush();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

Related

  1. getDouble(javax.swing.JComboBox input)
  2. getIfHasValue(Object val, JComboBox cb)
  3. getInt(JComboBox combo)
  4. IsComboBoxModified(JComboBox comboBox, Object originalValue)
  5. replaceContents(JComboBox combob, Object[] values)
  6. setComboBoxIndex(final JComboBox aComboBox, final int aIndex)
  7. value(JComboBox cb, Object[] val)