Java Code Examples for javax.swing.JComboBox#setModel()

The following examples show how to use javax.swing.JComboBox#setModel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GUIRegistrationPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void createPositionModel(final JComboBox positionsCombo,
        final FileObject[] files,
        final LayerItemPresenter parent) {
    DefaultComboBoxModel<Position> newModel = new DefaultComboBoxModel<>();
    LayerItemPresenter previous = null;
    for (FileObject file : files) {
        if (file.getNameExt().endsWith(LayerUtil.HIDDEN)) {
            continue;
        }
        LayerItemPresenter current = new LayerItemPresenter(
                file,
                parent.getFileObject());
        newModel.addElement(createPosition(previous, current));
        previous = current;
    }
    newModel.addElement(createPosition(previous, null));
    positionsCombo.setModel(newModel);
    checkValidity();
}
 
Example 2
Source File: XgappUpgradeSelector.java    From gate-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected UpgradeStrategyEditor() {
  super(new JComboBox<UpgradeXGAPP.UpgradePath.UpgradeStrategy>());
  JComboBox<UpgradeXGAPP.UpgradePath.UpgradeStrategy> combo = (JComboBox)getComponent();
  combo.setRenderer(new DefaultListCellRenderer() {
    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
      JLabel renderer = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
      renderer.setText(((UpgradeXGAPP.UpgradePath.UpgradeStrategy)value).label);
      renderer.setToolTipText(((UpgradeXGAPP.UpgradePath.UpgradeStrategy)value).tooltip);
      renderer.setIcon(strategyIcons.get(value));
      renderer.setDisabledIcon(disabledStrategyIcon);
      return renderer;
    }
  });
  combo.setModel(new DefaultComboBoxModel<>());
}
 
Example 3
Source File: FmtOptions.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Very smart method which tries to set the values in the components correctly
 */
private void loadData( JComponent jc, String optionID, Preferences node ) {

    if ( jc instanceof JTextField ) {
        JTextField field = (JTextField)jc;
        field.setText( node.get(optionID, provider.getDefaultAsString(optionID)) );
    }
    else if ( jc instanceof JCheckBox ) {
        JCheckBox checkBox = (JCheckBox)jc;
        boolean df = provider.getDefaultAsBoolean(optionID);
        checkBox.setSelected( node.getBoolean(optionID, df));
    }
    else if ( jc instanceof JComboBox) {
        JComboBox cb  = (JComboBox)jc;
        String value = node.get(optionID, provider.getDefaultAsString(optionID) );
        ComboBoxModel model = createModel(value);
        cb.setModel(model);
        ComboItem item = whichItem(value, model);
        cb.setSelectedItem(item);
    }

}
 
Example 4
Source File: ColorComboBox.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Creates a new instance of ColorChooser */
   static void init (final JComboBox<ColorValue> combo) {
       combo.setModel (new DefaultComboBoxModel<> (content));
       combo.setRenderer (new ColorComboBoxRenderer (combo));
       combo.setEditable (true);
       combo.setEditor (new ColorComboBoxRenderer (combo));
combo.setSelectedItem (new ColorValue (null, null));
       combo.addActionListener (new ComboBoxListener (combo));
   }
 
Example 5
Source File: MultiGradientTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private JComboBox createCombo(JPanel panel, Enum e) {
    JComboBox cmb = new JComboBox();
    cmb.setModel(new EnumComboBoxModel(e.getClass()));
    cmb.addActionListener(this);
    panel.add(cmb);
    return cmb;
}
 
Example 6
Source File: I2CExample.java    From firmata4j with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static String requestPort() {
    JComboBox<String> portNameSelector = new JComboBox<>();
    portNameSelector.setModel(new DefaultComboBoxModel<String>());
    String[] portNames;
    if (SerialNativeInterface.getOsType() == SerialNativeInterface.OS_MAC_OS_X) {
        // for MAC OS default pattern of jssc library is too restrictive
        portNames = SerialPortList.getPortNames("/dev/", Pattern.compile("tty\\..*"));
    } else {
        portNames = SerialPortList.getPortNames();
    }
    for (String portName : portNames) {
        portNameSelector.addItem(portName);
    }
    if (portNameSelector.getItemCount() == 0) {
        JOptionPane.showMessageDialog(null, "Cannot find any serial port", "Error", JOptionPane.ERROR_MESSAGE);
        System.exit(1);
    }
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    panel.add(new JLabel("Port "));
    panel.add(portNameSelector);
    if (JOptionPane.showConfirmDialog(null, panel, "Select the port", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
        return portNameSelector.getSelectedItem().toString();
    } else {
        System.exit(0);
    }
    return "";
}
 
Example 7
Source File: BaseRunCustomizer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void initServerModel(JComboBox serverCBox, JLabel serverLabel) {
    final List<Server> servers = ServerUtils.findServersFor(type);
    final Server defaultServer = ServerUtils.findServer(project);

    serverCBox.setModel(new DefaultComboBoxModel(servers.toArray()));
    
    serverUpdater = ComboBoxUpdater.create(serverCBox, serverLabel, defaultServer, new ComboBoxUpdater.Store() {

        @Override
        public void storeValue(Object newServer) {
            if (newServer == null) {
                JavaEEProjectSettings.setServerInstanceID(project, defaultServer.getServerInstanceID());
            } else {
                if (newServer instanceof Server) {
                    Server selectedServer = (Server) newServer;

                    String serverID = selectedServer.getServerID();
                    String serverInstanceID = selectedServer.getServerInstanceID();

                    // User is trying to set <No Server> option
                    if (ExecutionChecker.DEV_NULL.equals(serverInstanceID)) {
                        MavenProjectSupport.setServerID(project, null);
                        JavaEEProjectSettings.setServerInstanceID(project, null);

                    } else {
                        MavenProjectSupport.setServerID(project, serverID);
                        JavaEEProjectSettings.setServerInstanceID(project, serverInstanceID);
                    }
                    MavenProjectSupport.changeServer(project, false);
                }
            }
        }
    });
}
 
Example 8
Source File: LuceneDataStoreSearchGUI.java    From gate-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void updateAnnotationSetsList() {
  String corpusName =
          (corpusToSearchIn.getSelectedItem()
                  .equals(Constants.ENTIRE_DATASTORE))
                  ? null
                  : (String)corpusIds
                          .get(corpusToSearchIn.getSelectedIndex() - 1);
  TreeSet<String> ts = new TreeSet<String>(stringCollator);
  ts.addAll(getAnnotationSetNames(corpusName));
  DefaultComboBoxModel<String> dcbm = new DefaultComboBoxModel<String>(ts.toArray(new String[ts.size()]));
  dcbm.insertElementAt(Constants.ALL_SETS, 0);
  annotationSetsToSearchIn.setModel(dcbm);
  annotationSetsToSearchIn.setSelectedItem(Constants.ALL_SETS);

  // used in the ConfigureStackViewFrame as Annotation type column
  // cell editor
  TreeSet<String> types = new TreeSet<String>(stringCollator);
  types.addAll(getTypesAndFeatures(null, null).keySet());
  // put all annotation types from the datastore
  // combobox used as cell editor
  JComboBox<String> annotTypesBox = new JComboBox<String>();
  annotTypesBox.setMaximumRowCount(10);
  annotTypesBox.setModel(new DefaultComboBoxModel<String>(types.toArray(new String[types.size()])));
  DefaultCellEditor cellEditor = new DefaultCellEditor(annotTypesBox);
  cellEditor.setClickCountToStart(0);
  configureStackViewFrame.getTable().getColumnModel()
          .getColumn(ANNOTATION_TYPE).setCellEditor(cellEditor);
}
 
Example 9
Source File: AbstractUnitOptionUI.java    From freecol with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void itemStateChanged(ItemEvent e) {
    // When the unit type changes, we have to reset the role choices
    JComboBox<String> box = this.roleUI.getComponent();
    DefaultComboBoxModel<String> model;
    boolean enable = false;
    UnitType type = (UnitType)this.typeUI.getComponent().getSelectedItem();
    if (type != null && type.hasAbility(Ability.CAN_BE_EQUIPPED)) {
        final Specification spec = type.getSpecification();
        final NationType nt = getOption().getNationType();
        int n = 0;
        model = new DefaultComboBoxModel<>();
        for (String ri : getOption().getRole().getChoices()) {
            Role role = spec.getRole(ri);
            if (role.isAvailableTo(type, nt)) {
                model.addElement(ri);
                n++;
            }
        }
        enable = n > 1 && isEditable();
    } else {
        model = new DefaultComboBoxModel<>(new String[] {
                Specification.DEFAULT_ROLE_ID });
    }
    box.setModel(model);
    box.setEnabled(enable);
}
 
Example 10
Source File: Example.java    From firmata4j with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static String requestPort() {
    JComboBox<String> portNameSelector = new JComboBox<>();
    portNameSelector.setModel(new DefaultComboBoxModel<String>());
    String[] portNames;
    if (SerialNativeInterface.getOsType() == SerialNativeInterface.OS_MAC_OS_X) {
        // for MAC OS default pattern of jssc library is too restrictive
        portNames = SerialPortList.getPortNames("/dev/", Pattern.compile("tty\\..*"));
    } else {
        portNames = SerialPortList.getPortNames();
    }
    for (String portName : portNames) {
        portNameSelector.addItem(portName);
    }
    if (portNameSelector.getItemCount() == 0) {
        JOptionPane.showMessageDialog(null, "Cannot find any serial port", "Error", JOptionPane.ERROR_MESSAGE);
        System.exit(1);
    }
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    panel.add(new JLabel("Port "));
    panel.add(portNameSelector);
    if (JOptionPane.showConfirmDialog(null, panel, "Select the port", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
        return portNameSelector.getSelectedItem().toString();
    } else {
        System.exit(0);
    }
    return "";
}
 
Example 11
Source File: MultiGradientTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private JComboBox createCombo(JPanel panel, Enum e) {
    JComboBox cmb = new JComboBox();
    cmb.setModel(new EnumComboBoxModel(e.getClass()));
    cmb.addActionListener(this);
    panel.add(cmb);
    return cmb;
}
 
Example 12
Source File: MultiGradientTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private JComboBox createCombo(JPanel panel, Enum e) {
    JComboBox cmb = new JComboBox();
    cmb.setModel(new EnumComboBoxModel(e.getClass()));
    cmb.addActionListener(this);
    panel.add(cmb);
    return cmb;
}
 
Example 13
Source File: ComponentController.java    From swingsane with Apache License 2.0 5 votes vote down vote up
private void updateSourceModel(ComboBoxModel<String> sourceModel, String source) {
  JComboBox<String> sourceComboBox = components.getSourceComboBox();
  sourceComboBox.setModel(sourceModel != null ? sourceModel : new DefaultComboBoxModel<String>());
  sourceComboBox.setEnabled(sourceModel != null ? true : false);
  if (source != null) {
    sourceComboBox.setSelectedItem(source);
  }
}
 
Example 14
Source File: DeckChooserDialog.java    From magarena with GNU General Public License v3.0 5 votes vote down vote up
private JComboBox<DeckType> getDeckTypeComboBox() {
    final JComboBox<DeckType> cbo = new JComboBox<>();
    cbo.setModel(new DefaultComboBoxModel<>(DeckType.getDuelDeckTypes()));
    cbo.setLightWeightPopupEnabled(false);
    cbo.setFocusable(false);
    cbo.setFont(FontsAndBorders.FONT2);
    ((JLabel)cbo.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER);
    return cbo;
}
 
Example 15
Source File: MultiGradientTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private JComboBox createCombo(JPanel panel, Enum e) {
    JComboBox cmb = new JComboBox();
    cmb.setModel(new EnumComboBoxModel(e.getClass()));
    cmb.addActionListener(this);
    panel.add(cmb);
    return cmb;
}
 
Example 16
Source File: MultiGradientTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private JComboBox createCombo(JPanel panel, Enum e) {
    JComboBox cmb = new JComboBox();
    cmb.setModel(new EnumComboBoxModel(e.getClass()));
    cmb.addActionListener(this);
    panel.add(cmb);
    return cmb;
}
 
Example 17
Source File: UpdateSummaryTable.java    From bigtable-sql with Apache License 2.0 5 votes vote down vote up
private void setModel(JComboBox box, ArtifactAction... actions) {
	ComboBoxModel oldModel = box.getModel();
	box.setModel(getComboBoxModel(actions));
	if (oldModel.getSize() != actions.length) {
		box.firePropertyChange("itemCount", oldModel.getSize(), actions.length);
	}
}
 
Example 18
Source File: SelectionPropertyCellEditor.java    From openAGV with Apache License 2.0 5 votes vote down vote up
@Override
public Component getTableCellEditorComponent(
    JTable table, Object value, boolean isSelected, int row, int column) {

  setValue(value);
  JComboBox<Object> comboBox = getComponent();
  comboBox.setModel(new DefaultComboBoxModel<>(((Selectable) property()).getPossibleValues().toArray()));
  comboBox.setSelectedItem(property().getValue());

  return fComponent;
}
 
Example 19
Source File: ConnectionAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void initComponents() {
    setLayout(new BorderLayout(4, 0));
    setBorder(new EmptyBorder(0, 2, 0, 8));
    setOpaque(false);
    setFocusTraversalPolicyProvider(true);
    setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
        @Override
        public Component getDefaultComponent(Container aContainer) {
            if (!SwingUtilities.isEventDispatchThread()) {
                return null;
            }
            final EditorCookie ec = actionContext.lookup(
                    EditorCookie.class);
            if (ec != null) {
                JEditorPane[] panes = ec.getOpenedPanes();
                if (panes != null) {
                    for (JEditorPane pane : panes) {
                        if (pane.isShowing()) {
                            return pane;
                        }
                    }
                }
            }

            return null;
        }
   });

    combo = new JComboBox();
    combo.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            DatabaseConnection dbconn = (DatabaseConnection)combo.getSelectedItem();
            combo.setToolTipText(dbconn != null ? dbconn.getDisplayName() : null);
        }
    });
    combo.setOpaque(false);
    combo.setModel(new DefaultComboBoxModel(
            new String[] { NbBundle.getMessage(ToolbarPresenter.class, "ConnectionAction.ToolbarPresenter.LoadingConnections") }));

    combo.setRenderer(new DatabaseConnectionRenderer());
    String accessibleName = NbBundle.getMessage(ConnectionAction.class, "LBL_DatabaseConnection");
    combo.getAccessibleContext().setAccessibleName(accessibleName);
    combo.getAccessibleContext().setAccessibleDescription(accessibleName);
    combo.setPreferredSize (new Dimension (400, combo.getPreferredSize ().height));

    add(combo, BorderLayout.CENTER);

    comboLabel = new JLabel();
    Mnemonics.setLocalizedText(comboLabel, NbBundle.getMessage(ConnectionAction.class, "LBL_ConnectionAction"));
    comboLabel.setOpaque(false);
    comboLabel.setLabelFor(combo);
    add(comboLabel, BorderLayout.WEST);
}
 
Example 20
Source File: SourceGroupUISupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static void connect(JComboBox comboBox, SourceGroup[] sourceGroups) {
    comboBox.setModel(new DefaultComboBoxModel(sourceGroups));
    comboBox.setRenderer(new SourceGroupRenderer());
}