Java tutorial
//package com.java2s; /* * OpenBench LogicSniffer / SUMP project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * * * Copyright (C) 2010-2011 - J.W. Janssen, http://www.lxtreme.nl */ import javax.swing.*; public class Main { /** * Creates a channel selector combobox, where only a valid channel can be * selected. * * @param aChannelCount * the number of channels to include in the combobox options; * @return a combobox with channel selector options. */ public static JComboBox createChannelSelector(final int aChannelCount) { return internalCreateChannelSelector(aChannelCount, -1, false /* aAddUnusedOption */); } /** * Creates a channel selector combobox, where only a valid channel can be * selected. * * @param aChannelCount * the number of channels to include in the combobox options; * @param aDefaultSelectedIndex * the default selected index for the created combobox. * @return a combobox with channel selector options. */ public static JComboBox createChannelSelector(final int aChannelCount, final int aDefaultSelectedIndex) { return internalCreateChannelSelector(aChannelCount, aDefaultSelectedIndex, false /* aAddUnusedOption */); } /** * Creates a channel selector combobox. * * @param aChannelCount * the number of channels to include in the combobox options; * @param aDefaultSelectedindex * the default selected index; * @param aAddUnusedOption * <code>true</code> to add "unused" as first option, * <code>false</code> to omit this option. * @return a combobox with channel selector options. */ private static JComboBox internalCreateChannelSelector(final int aChannelCount, final int aDefaultSelectedindex, final boolean aAddUnusedOption) { int modelSize = Math.max(0, Math.min(32, aChannelCount)); if (aAddUnusedOption) { modelSize++; } final String dataChannels[] = new String[modelSize]; int i = 0; if (aAddUnusedOption) { dataChannels[i++] = "Unused"; } for (; i < modelSize; i++) { final int index = aAddUnusedOption ? i - 1 : i; dataChannels[i] = String.format("Channel %d", Integer.valueOf(index)); } int selectedIndex = aDefaultSelectedindex < 0 ? 0 : aDefaultSelectedindex % modelSize; final JComboBox result = new JComboBox(dataChannels); result.setSelectedIndex(selectedIndex); return result; } }