下面列出了javax.swing.JComboBox#setModel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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<>());
}
/** 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);
}
}
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();
}
@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;
}
/** 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));
}
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);
}
}
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;
}
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;
}
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;
}
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);
}
}
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;
}
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;
}
@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 "";
}
/**
* {@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);
}
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);
}
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);
}
}
}
});
}
@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 "";
}
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;
}
public static void connect(JComboBox comboBox, SourceGroup[] sourceGroups) {
comboBox.setModel(new DefaultComboBoxModel(sourceGroups));
comboBox.setRenderer(new SourceGroupRenderer());
}
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);
}