List of usage examples for javax.swing JComboBox setModel
@BeanProperty(description = "Model that the combo box uses to get data to display.") public void setModel(ComboBoxModel<E> aModel)
JComboBox
uses to obtain the list of items. From source file:org.openmicroscopy.shoola.agents.fsimporter.chooser.LocationDialog.java
/** * Populates the JComboBox with the user details provided, * selecting the logged in user and attaching the item listener. * //from ww w . j a v a 2s . c o m * @param comboBox The JComboBox to populate * @param group The group being displayed * @param itemListener An item listener to add for the JComboBox * @param userID The id of the user. */ private void displayUsers(JComboBox comboBox, GroupData group, ItemListener itemListener, long userID) { if (comboBox == null || group == null) return; if (itemListener != null) comboBox.removeItemListener(itemListener); comboBox.removeAllItems(); DefaultComboBoxModel model = new SelectableComboBoxModel(); Selectable<ExperimenterDisplay> selected = null; List<ExperimenterData> members = sort(group.getExperimenters()); boolean canImportAs; Selectable<ExperimenterDisplay> item; List<String> tooltips = new ArrayList<String>(members.size()); List<String> lines; for (ExperimenterData user : members) { canImportAs = canImportForUserInGroup(user, group); item = new Selectable<ExperimenterDisplay>(new ExperimenterDisplay(user), canImportAs); if (user.getId() == userID) selected = item; lines = new ArrayList<String>(); lines.addAll(UIUtilities.wrapStyleWord(EditorUtil.formatExperimenter(user))); tooltips.add(UIUtilities.formatToolTipText(lines)); model.addElement(item); } ComboBoxToolTipRenderer renderer = createComboboxRenderer(); renderer.setTooltips(tooltips); comboBox.setModel(model); comboBox.setRenderer(renderer); if (selected != null) comboBox.setSelectedItem(selected); if (itemListener != null) comboBox.addItemListener(itemListener); }
From source file:org.pentaho.ui.xul.swing.tags.SwingTree.java
private TableCellRenderer getCellRenderer(final SwingTreeCol col) { return new DefaultTableCellRenderer() { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { ColumnType colType = col.getColumnType(); if (colType == ColumnType.DYNAMIC) { colType = ColumnType.valueOf(extractDynamicColType(elements.toArray()[row], column)); }// w ww .j a v a 2 s .com final XulTreeCell cell = getRootChildren().getItem(row).getRow().getCell(column); switch (colType) { case CHECKBOX: JCheckBox checkbox = new JCheckBox(); if (value instanceof String) { checkbox.setSelected(((String) value).equalsIgnoreCase("true")); //$NON-NLS-1$ } else if (value instanceof Boolean) { checkbox.setSelected((Boolean) value); } else if (value == null) { checkbox.setSelected(false); } if (isSelected) { checkbox.setBackground(Color.LIGHT_GRAY); } checkbox.setEnabled(!cell.isDisabled()); return checkbox; case COMBOBOX: case EDITABLECOMBOBOX: Vector data; final JComboBox comboBox = new JComboBox(); if (cell == null) { } else { data = (cell.getValue() != null) ? (Vector) cell.getValue() : new Vector(); if (data == null) { logger.debug("SwingTreeCell combobox data is null, passed in value: " + value); //$NON-NLS-1$ if (value instanceof Vector) { data = (Vector) value; } } if (data != null) { comboBox.setModel(new DefaultComboBoxModel(data)); try { comboBox.setSelectedIndex(cell.getSelectedIndex()); } catch (Exception e) { logger.error("error setting selected index on the combobox editor"); //$NON-NLS-1$ } } } if (colType == ColumnType.EDITABLECOMBOBOX) { comboBox.setEditable(true); ((JTextComponent) comboBox.getEditor().getEditorComponent()).setText(cell.getLabel()); } if (isSelected) { comboBox.setBackground(Color.LIGHT_GRAY); } comboBox.setEnabled(!cell.isDisabled()); return comboBox; case CUSTOM: return new CustomCellEditorWrapper(cell, customEditors.get(col.getType())); default: JLabel label = new JLabel((String) value); if (isSelected) { label.setOpaque(true); label.setBackground(Color.LIGHT_GRAY); } return label; } } }; }
From source file:org.springframework.richclient.factory.DefaultComponentFactory.java
public void configureForEnum(JComboBox comboBox, Class enumType) { Collection enumValues = getEnumResolver().getLabeledEnumSet(enumType); if (logger.isDebugEnabled()) { logger.debug("Populating combo box model with enums of type '" + enumType.getName() + "'; enums are [" + enumValues + "]"); }/* w ww .j a v a 2 s. c o m*/ CompoundComparator comparator = new CompoundComparator(); comparator.addComparator(LabeledEnum.LABEL_ORDER); comparator.addComparator(new ComparableComparator()); comboBox.setModel(new ComboBoxListModel(new ArrayList(enumValues), comparator)); comboBox.setRenderer(new LabeledEnumListRenderer(messageSource)); comboBox.setEditor(new LabeledEnumComboBoxEditor(messageSource, comboBox.getEditor())); }