Java Code Examples for javax.swing.DefaultComboBoxModel#setSelectedItem()

The following examples show how to use javax.swing.DefaultComboBoxModel#setSelectedItem() . 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: SelectProjectPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void btnProjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProjectActionPerformed
    JFileChooser chooser = ProjectChooser.projectChooser();
    int res = chooser.showOpenDialog(SwingUtilities.getWindowAncestor(this));
    if (res == JFileChooser.APPROVE_OPTION) {
        File fil = chooser.getSelectedFile();
        FileObject fo = FileUtil.toFileObject(fil);
        if (fo != null) {
            try {
                Project p = ProjectManager.getDefault().findProject(fo);
                DefaultComboBoxModel model = (DefaultComboBoxModel)comProject.getModel();
                model.addElement(p);
                model.setSelectedItem(p);
                if (EMPTY == model.getElementAt(0)) {
                    model.removeElement(EMPTY);
                }
            } catch (IOException exc) {
                ErrorManager.getDefault().notify(exc);
            }
        }
    }
}
 
Example 2
Source File: AbstractSwingGuiCallback.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private JComboBox<String> createComboBox(final List<FormItem> items, FormItem item) {
    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
    JComboBox<String> box = new JComboBox<>(model);
    item.setField(box);
    for (String possibleValue : item.getPossibleValues()) {
        model.addElement(possibleValue);
    }
    String defaultValue = item.getDefaultValue();
    if (defaultValue == null) {
        model.setSelectedItem(model.getElementAt(0));
    } else {
        model.setSelectedItem(defaultValue);
    }
    box.addActionListener(e -> updateFormItemsFromGui(items));
    return box;
}
 
Example 3
Source File: ColorComboBox.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Creates a new color combobox and populates it with the excel colors.
 */
public ColorComboBox() {
  final DefaultComboBoxModel model = new DefaultComboBoxModel( ColorUtility.getPredefinedExcelColors() );
  model.insertElementAt( null, 0 );
  model.setSelectedItem( null );

  setModel( model );
  setRenderer( new ColorCellRenderer() );
  final int height1 = getPreferredSize().height;
  setMaximumSize( new Dimension( height1 * 4, height1 ) );
  setFocusable( false );

  final boolean isGTK = isGTKLookAndFeel();
  setEditable( isGTK );

  // if it's GTK LnF, we have to customize the combo box since GTK Lnf ignores the <i>setBackground</i>
  if ( isGTK ) {
    setEditor( new CustomGTKComboBoxEditor() );
  }
}
 
Example 4
Source File: SummaryTablePanel.java    From nmonvisualizer with Apache License 2.0 6 votes vote down vote up
private void updateStatisticsComboBox() {
    String newName = Statistic.GRANULARITY_MAXIMUM.getName(gui.getGranularity());

    @SuppressWarnings("unchecked")
    DefaultComboBoxModel<Object> model = (DefaultComboBoxModel<Object>) ((JComboBox<Object>) statsPanel
            .getComponent(1)).getModel();

    boolean reselect = false;

    if (model.getSelectedItem() == model.getElementAt(4)) {
        reselect = true;
    }

    model.removeElementAt(4);
    model.insertElementAt(newName, 4);

    if (reselect) {
        model.setSelectedItem(newName);
    }
}
 
Example 5
Source File: ConnectionsPanel.java    From desktop with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void load() {
    // Fill combo box
    DefaultComboBoxModel cmbModel = new DefaultComboBoxModel();    
    cmbModel.addElement(new ComboBoxItem(null));
	
    for (PluginInfo pluginInfo: Plugins.list()){
        ComboBoxItem element = new ComboBoxItem(pluginInfo);
        cmbModel.addElement(element);
        if(connection != null && connection.getPluginInfo().getName().compareTo(pluginInfo.getName()) == 0){
            cmbModel.setSelectedItem(element);
        }
    }

    cmbPlugins.setModel(cmbModel);        
}
 
Example 6
Source File: GlyphsFrame.java    From constellation with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void textActionPerformed() {
    final String text = textField.getText().trim();
    final DefaultComboBoxModel<String> model = (DefaultComboBoxModel) textLines.getModel();
    model.addElement(text);
    model.setSelectedItem(text);
    repaint();
}
 
Example 7
Source File: ClassNamePanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates new form ClassNamePanel
 */
public ClassNamePanel(Project project, FileObject anchor, String initialName) {
    this.project = project;
    this.anchor = anchor;
    initComponents();
    
    Project openProjects[] = OpenProjects.getDefault().getOpenProjects();
    Arrays.sort( openProjects, new ProjectByDisplayNameComparator());
    DefaultComboBoxModel projectsModel = new DefaultComboBoxModel( openProjects );
    projectSelector.setModel( projectsModel );                
    if (project != null) {
        projectSelector.setSelectedItem( project );
        projectSelector.setEnabled(false);
        projectLabel.setEnabled(false);
    } else if (projectsModel.getSize() > 0) {
        this.project = (Project)projectsModel.getElementAt(0);
        projectsModel.setSelectedItem(this.project);
    }
    projectSelector.setRenderer(new ProjectCellRenderer());
    locationSelect.setRenderer(new GroupCellRenderer());
    packageSelect.setRenderer(PackageView.listRenderer());
    
    updateRoots();
    updatePackages();
    
    selectInitialPackage();
    
    ActionListener al = this::actionPerformed;
    locationSelect.addActionListener(al);
    packageSelect.addActionListener(al);
    packageSelect.getEditor().addActionListener(al);
    projectSelector.addActionListener(al);
    className.getDocument().addDocumentListener(this);
    
    if (initialName != null) {
        className.setText(initialName);
    }
}
 
Example 8
Source File: OJETPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void initData() {
    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
    for (String version: DataProviderImpl.getInstance().getAvailableVersions()) {
        model.addElement(version);
    }
    model.setSelectedItem(OJETPreferences.get(project, OJETPreferences.VERSION));
    cbVersion.setModel(model);
    category.setStoreListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            save();
        }
    } );
}
 
Example 9
Source File: ECMAScriptPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void initData() {
    DefaultComboBoxModel model = new DefaultComboBoxModel();
    for (JsVersion version: JsPreferences.getECMAScriptAvailableVersions()) {
        model.addElement(new DisplayVersion(version));
    }
    model.setSelectedItem(new DisplayVersion(JsPreferences.getECMAScriptVersion(project)));
    cbVersion.setModel(model);
    allowJsonComments.setSelected(JsonPreferences.forProject(project).isCommentSupported());
    category.setStoreListener((e) -> save());
}
 
Example 10
Source File: EditorControl.java    From settlers-remake with MIT License 5 votes vote down vote up
/**
 * Update the player selection combobox
 */
private void updatePlayerCombobox() {
	// create a new model, because a swing bug there are sometimes problems updating an existing model
	DefaultComboBoxModel<Integer> model = new DefaultComboBoxModel<>();
	model.setSelectedItem(playerCombobox.getSelectedItem());
	for (int i = 0; i < mapData.getPlayerCount(); i++) {
		model.addElement(i);
	}
	playerCombobox.setModel(model);
}
 
Example 11
Source File: JdbcDataSourceDialog.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void actionPerformed( final ActionEvent e ) {
  final NamedDataSourceDialogModel dialogModel = getDialogModel();
  final DefaultComboBoxModel queries = dialogModel.getQueries();
  queries.removeElement( queries.getSelectedItem() );
  if ( queryNameList.getLastVisibleIndex() != -1 ) {
    queryNameList.setSelectedValue( dialogModel.getQueries().getQuery( queryNameList.getLastVisibleIndex() ), true );
    queryNameList.setSelectedIndex( queryNameList.getLastVisibleIndex() );
    queries.setSelectedItem( dialogModel.getQueries().getQuery( queryNameList.getLastVisibleIndex( ) ) );
    queryTextArea.setEnabled( true );
  } else {
    queries.setSelectedItem( null );
    queryNameList.clearSelection();
    queryTextArea.setEnabled( false );
  }
}
 
Example 12
Source File: RepositoryOpenDialog.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
private ComboBoxModel createLocationModel( final FileObject selectedFolder ) {
  if ( fileSystemRoot == null ) {
    return new DefaultComboBoxModel();
  }

  try {
    final ArrayList<FileObject> list = new ArrayList<FileObject>();
    FileObject folder = selectedFolder;
    while ( folder != null ) {
      if ( fileSystemRoot.equals( folder ) ) {
        break;
      }

      if ( folder.getType() != FileType.FILE ) {
        list.add( folder );
      }

      final FileObject parent = folder.getParent();
      if ( folder.equals( parent ) ) {
        // protect yourself against infinite loops ..
        break;
      }
      folder = parent;
    }
    list.add( fileSystemRoot );
    final DefaultComboBoxModel model = new DefaultComboBoxModel( list.toArray() );
    model.setSelectedItem( list.get( 0 ) );
    return model;
  } catch ( FileSystemException e ) {
    return new DefaultComboBoxModel();
  }
}
 
Example 13
Source File: TargetPanel.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Load all targeted factory configuration instances and update user interface
 * to show them.
 *
 * @param selectedPid
 *          If this PID is available then select it, otherwise select the last
 *          PID. If {@link #nextFactoryPidToSelect} is non-null then select
 *          that configuration instance and set the field to {@code null}.
 */
private void updateSelectionFactoryPID(String selectedPid)
{
  synchronized (pid2Cfg) {
    pid2Cfg.clear();
    if (selectedPid == null) {
      selectedPid = "";
    }

    for (int i = 0; i < MAX_SIZE; i++) {
      try {
        final Configuration[] configs =
          CMDisplayer.getCA().listConfigurations("(service.factoryPid="
                                                     + targetedPids[i] + ")");
        if (configs != null) {
          for (final Configuration cfg : configs) {
            pid2Cfg.put(cfg.getPid(), cfg);
          }
        }
      } catch (final Exception e) {
        Activator.log
            .error("Faile to load factory configuration instances for fpid '"
                   + targetedPids[i] + "': " + e.getMessage(), e);
      }
    }

    final SortedSet<String> instancePIDs =
      new TreeSet<String>(pid2Cfg.keySet());
    instancePIDs.add(FACTORY_PID_DEFAULTS);

    final DefaultComboBoxModel model =
      new DefaultComboBoxModel(instancePIDs.toArray());
    if (nextFactoryPidToSelect != null) {
      if (instancePIDs.contains(nextFactoryPidToSelect)) {
        selectedPid = nextFactoryPidToSelect;
      }
      nextFactoryPidToSelect = null;
    } else if (!instancePIDs.contains(selectedPid)) {
      // New selection needed, use last PID.
      selectedPid = (String) model.getElementAt(model.getSize() - 1);
    }
    model.setSelectedItem(selectedPid);
    fbox.setModel(model);
    final Configuration selectedCfg = pid2Cfg.get(selectedPid);

    // Update the targeted PID selectors to match the target selectors in the
    // factory PID of the selected instance.
    final String fpid =
      selectedCfg != null ? selectedCfg.getFactoryPid() : targetedPids[0];
    String tpid = null;
    for (int i = 0; i < MAX_SIZE && null != (tpid = targetedPids[i]); i++) {
      rbs[i].setToolTipText(TARGET_LEVEL_FACOTRY_PID_TOOLTIPS[i] + tpid
                            + "</code></p></html>");

      if (fpid.equals(targetedPids[i])) {
        rbs[i].setSelected(true);
        selectedTargetLevel = i;
        if (selectedCfg != null) {
          icons[i].setIcon(openDocumentIcon);
          icons[i].setToolTipText("exists");
        } else {
          icons[i].setIcon(newDocumentIcon);
          icons[i].setToolTipText("to be created");
        }
      } else {
        icons[i].setIcon(newDocumentIcon);
        icons[i].setToolTipText("to be created");
      }
    }
  }
}
 
Example 14
Source File: UnitSelectorDialog.java    From megamek with GNU General Public License v2.0 4 votes vote down vote up
private void  updateTypeCombo() {
    lstTechLevel.removeListSelectionListener(this);
    int[] selectedIndices = lstTechLevel.getSelectedIndices();
    int gameTL;
    if (client != null) {
        gameTL = TechConstants.getSimpleLevel(client.getGame().getOptions()
                .stringOption("techlevel"));
    } else {
        gameTL = TechConstants.T_SIMPLE_UNOFFICIAL;
    }

    int maxTech;
    switch (gameTL) {
        case TechConstants.T_SIMPLE_INTRO:
            maxTech = TechConstants.T_INTRO_BOXSET;
            break;
        case TechConstants.T_SIMPLE_STANDARD:
            maxTech = TechConstants.T_TW_ALL;
            break;
        case TechConstants.T_SIMPLE_ADVANCED:
            maxTech = TechConstants.T_CLAN_ADVANCED;
            break;
        case TechConstants.T_SIMPLE_EXPERIMENTAL:
            maxTech = TechConstants.T_CLAN_EXPERIMENTAL;
            break;
        case TechConstants.T_SIMPLE_UNOFFICIAL:
            maxTech = TechConstants.T_CLAN_UNOFFICIAL;
            break;
        default:
            maxTech = TechConstants.T_CLAN_UNOFFICIAL;
    }

    tlLstToIdx.clear();
    DefaultComboBoxModel<String> techModel = new DefaultComboBoxModel<>();
    int selectionIdx = 0;
    for (int tl = 0; tl <= maxTech; tl++) {
        if ((tl != TechConstants.T_IS_TW_ALL)
                && (tl != TechConstants.T_TW_ALL)) {
            tlLstToIdx.put(selectionIdx, tl);
            techModel.addElement(TechConstants.getLevelDisplayableName(tl));
            selectionIdx++;
        }
    }
    techModel.setSelectedItem(TechConstants.getLevelDisplayableName(0));
    lstTechLevel.setModel(techModel);

    lstTechLevel.setSelectedIndices(selectedIndices);
    lstTechLevel.addListSelectionListener(this);
}
 
Example 15
Source File: QueryRemoveAction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void actionPerformed( final ActionEvent e ) {
  final DefaultComboBoxModel comboBoxModel = queries.getQueries();
  comboBoxModel.removeElement( comboBoxModel.getSelectedItem() );
  comboBoxModel.setSelectedItem( null );
}