下面列出了怎么用javax.swing.ComboBoxModel的API类实例代码及写法,或者点击链接到github查看源代码。
private ComboBoxModel getListObjDoc()
{
Vector VObjects=new Vector();
try {
DriverGeneric Session=MainWin.getSession();
PDObjDefs Obj = new PDObjDefs(Session);
Cursor CursorId = Obj.getListDocs();
Record Res=Session.NextRec(CursorId);
while (Res!=null)
{
Attribute Attr=Res.getAttr(PDObjDefs.fNAME);
VObjects.add(Attr.getValue());
Res=Session.NextRec(CursorId);
}
Session.CloseCursor(CursorId);
} catch (PDException ex)
{
MainWin.Message("Error"+ex.getLocalizedMessage());
}
return(new DefaultComboBoxModel(VObjects));
}
private void updatePackages() {
final Object item = createdLocationComboBox.getSelectedItem();
if (!(item instanceof SourceGroupSupport.SourceGroupProxy)) {
return;
}
WAIT_MODEL.setSelectedItem(createdPackageComboBox.getEditor().getItem());
createdPackageComboBox.setModel(WAIT_MODEL);
if (updatePackagesTask != null) {
updatePackagesTask.cancel();
}
updatePackagesTask = new RequestProcessor("ComboUpdatePackages").post(new Runnable() { // NOI18N
@Override
public void run() {
final ComboBoxModel model = ((SourceGroupSupport.SourceGroupProxy) item).getPackagesComboBoxModel();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
model.setSelectedItem(createdPackageComboBox.getEditor().getItem());
createdPackageComboBox.setModel(model);
}
});
}
});
}
private ComboBoxModel getListObjDoc()
{
Vector VObjects=new Vector();
try {
DriverGeneric Session=MainWin.getSession();
PDObjDefs Obj = new PDObjDefs(Session);
Cursor CursorId = Obj.getListDocs();
Record Res=Session.NextRec(CursorId);
while (Res!=null)
{
Attribute Attr=Res.getAttr(PDObjDefs.fNAME);
VObjects.add(Attr.getValue());
Res=Session.NextRec(CursorId);
}
Session.CloseCursor(CursorId);
} catch (PDException ex)
{
MainWin.Message("Error"+ex.getLocalizedMessage());
}
return(new DefaultComboBoxModel(VObjects));
}
private ComboBoxModel getListObjFold()
{
Vector VObjects=new Vector();
try {
DriverGeneric Session=MainWin.getSession();
PDObjDefs Obj = new PDObjDefs(Session);
Cursor CursorId = Obj.getListFold();
Record Res=Session.NextRec(CursorId);
while (Res!=null)
{
Attribute Attr=Res.getAttr(PDObjDefs.fNAME);
VObjects.add(Attr.getValue());
Res=Session.NextRec(CursorId);
}
Session.CloseCursor(CursorId);
} catch (PDException ex)
{
MainWin.Message("Error"+ex.getLocalizedMessage());
}
return(new DefaultComboBoxModel(VObjects));
}
public AutoCompletionComboBox(boolean caseSensitive, int preferredWidth, int preferredHeight, boolean wide,
ComboBoxModel<E> model) {
super(preferredWidth, preferredHeight, wide, model);
this.caseSensitive = caseSensitive;
setEditable(true);
setEditor(getEditor());
addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
setSelectedItem(((JTextField) getEditor().getEditorComponent()).getText());
actionPerformed(new ActionEvent(this, 0, "editingStopped"));
}
});
}
/**
* Creates new form ShowEffPomDiffPanel
*/
public ShowEffPomDiffPanel(ProjectConfigurationProvider<MavenConfiguration> configs) {
initComponents();
ComboBoxModel<MavenConfiguration> model = new DefaultComboBoxModel<MavenConfiguration>(configs.getConfigurations().toArray(new MavenConfiguration[0]));
comConfiguration.setModel(model);
comConfiguration.setEditable(false);
comConfiguration.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
return super.getListCellRendererComponent(list, ((ProjectConfiguration)value).getDisplayName(), index, isSelected, cellHasFocus);
}
});
comConfiguration.setSelectedItem(configs.getActiveConfiguration());
enableFields();
epProperties.setContentType("text/x-properties");
}
/**
* Select the scope
*
* @param projectName The name of project or null if find should be
* performed on all projects
*/
public void setScope(String projectName) {
JComboBoxOperator scopeOperator = getScope();
String select_item;
if (projectName == null) {
select_item = Bundle.getStringTrimmed("org.netbeans.modules.refactoring.java.ui.scope.Bundle", "LBL_AllProjects");
} else {
select_item = projectName;
}
ComboBoxModel model = scopeOperator.getModel();
int index = -1;
String dn;
for (int i = 0; i < model.getSize()-1; i++) { /// -1 ... it's custom and it fails
dn = ((org.netbeans.modules.refactoring.spi.impl.DelegatingScopeProvider) model.getElementAt(i)).getDisplayName();
if (dn.indexOf(select_item) != -1) {
index = i;
}
}
scopeOperator.selectItem(index);
}
/**
* Selects a given template.
*
* @param templatePath path of the template which should be selected;
* may be <code>null</code> - then no item is selected
*/
void selectTemplate(String templatePath) {
if (templatePath == null) {
return;
}
ComboBoxModel model = cboTemplate.getModel();
int itemsCount = model.getSize();
if (itemsCount == 0) {
return;
}
for (int i = 0; i < itemsCount; i++) {
NamedObject namedObj = (NamedObject) model.getElementAt(i);
FileObject template = (FileObject) namedObj.object;
if (template.getPath().equals(templatePath)) {
cboTemplate.setSelectedIndex(i);
return;
}
}
}
private ComboBoxModel getListObjDoc()
{
Vector VObjects=new Vector();
try {
DriverGeneric Session=MainWin.getSession();
PDObjDefs Obj = new PDObjDefs(Session);
Cursor CursorId = Obj.getListDocs();
Record Res=Session.NextRec(CursorId);
while (Res!=null)
{
Attribute Attr=Res.getAttr(PDObjDefs.fNAME);
VObjects.add(Attr.getValue());
Res=Session.NextRec(CursorId);
}
Session.CloseCursor(CursorId);
} catch (PDException ex)
{
MainWin.Message("Error"+ex.getLocalizedMessage());
}
return(new DefaultComboBoxModel(VObjects));
}
private void setValue(String value)
{
if (fieldComboBox != null)
{
ComboBoxModel<FieldValue> comboBoxModel = fieldComboBox.getModel();
for (int i = 0; i < comboBoxModel.getSize(); i++)
{
FieldValue fieldValue = comboBoxModel.getElementAt(i);
if (fieldValue.getEnumValue().equals(value))
{
fieldComboBox.setSelectedIndex(i);
}
}
}
else
{
fieldTextField.setText(value);
}
}
public static ComboBoxModel<String> getSourceModel(Scanner scanner) {
DefaultComboBoxModel<String> sourceModel = new DefaultComboBoxModel<String>();
HashMap<String, StringOption> stringOptions = scanner.getStringOptions();
StringOption stringOption = stringOptions.get(SANE_NAME_SCAN_SOURCE);
if (stringOption == null) {
return null;
}
Constraints constraints = stringOption.getConstraints();
List<String> values = constraints.getStringList();
for (String value : values) {
sourceModel.addElement(value);
}
if (values.size() > 0) {
return sourceModel;
} else {
return null;
}
}
private ComboBoxModel createRemoteBranchDropdownModel() {
final SortedComboBoxModel<GitRemoteBranch> sortedRemoteBranches
= new SortedComboBoxModel<GitRemoteBranch>(new TfGitHelper.BranchComparator());
final GitRemoteBranch remoteTrackingBranch = this.getRemoteTrackingBranch();
// only show valid remote branches
sortedRemoteBranches.addAll(Collections2.filter(getInfo().getRemoteBranches(),
remoteBranch -> {
/* two conditions:
* 1. remote must be a vso/tfs remote
* 2. this isn't the remote tracking branch of current local branch
*/
return tfGitRemotes.contains(remoteBranch.getRemote())
&& !remoteBranch.equals(remoteTrackingBranch);
})
);
sortedRemoteBranches.setSelectedItem(TfGitHelper.getDefaultBranch(sortedRemoteBranches.getItems(), tfGitRemotes));
return sortedRemoteBranches;
}
public void optionsLoaded(QuickStartParam quickStartParam) {
getSelectCheckBox().setSelected(quickStartParam.isAjaxSpiderEnabled());
String def = quickStartParam.getAjaxSpiderDefaultBrowser();
if (def == null || def.length() == 0) {
// no default
return;
}
ComboBoxModel<ProvidedBrowserUI> model = this.getBrowserComboBox().getModel();
for (int idx = 0; idx < model.getSize(); idx++) {
ProvidedBrowserUI el = model.getElementAt(idx);
if (el.getName().equals(def)) {
model.setSelectedItem(el);
break;
}
}
}
public void addLocalRepositoryFolderIfMissing(LocalRepositoryFolder localRepositoryFolder) {
ComboBoxModel<LocalRepositoryFolder> foldersModel = this.foldersComboBox.getModel();
boolean foundFolder = false;
for (int i = 0; i < foldersModel.getSize() && !foundFolder; i++) {
LocalRepositoryFolder existingFolder = foldersModel.getElementAt(i);
if (existingFolder != null && existingFolder.getId() == localRepositoryFolder.getId()) {
foundFolder = true;
}
}
if (!foundFolder) {
if (foldersModel.getSize() == 0) {
this.foldersComboBox.addItem(null);
}
this.foldersComboBox.addItem(localRepositoryFolder);
}
}
public Range getRangeFromFile() {
final ComboBoxModel<ColorPaletteWrapper> model = getModel();
final int selectedIndex = getSelectedIndex();
final ColorPaletteWrapper paletteWrapper = model.getElementAt(selectedIndex);
String name = paletteWrapper.name;
final ColorPaletteDef cpd;
if (name.startsWith(DERIVED_FROM)) {
name = name.substring(DERIVED_FROM.length()).trim();
if (name.toLowerCase().endsWith(".cpd")) {
name = FileUtils.getFilenameWithoutExtension(name);
}
cpd = findColorPalette(name);
} else {
cpd = paletteWrapper.cpd;
}
return new Range(cpd.getMinDisplaySample(), cpd.getMaxDisplaySample());
}
public void setSelectedView( final FileObject selectedView ) {
this.selectedView = selectedView;
if ( selectedView != null ) {
logger.debug( "Setting selected view to " + selectedView );
try {
if ( selectedView.getType() == FileType.FILE ) {
logger.debug( "Setting filename in selected view to " + selectedView.getName().getBaseName() );
this.fileNameTextField.setText( URLDecoder.decode( selectedView.getName().getBaseName(), "UTF-8" ) );
}
} catch ( Exception e ) {
// can be ignored ..
logger.debug( "Unable to determine file type. This is not fatal.", e );
}
final ComboBoxModel comboBoxModel = createLocationModel( selectedView );
this.locationCombo.setModel( comboBoxModel );
this.table.setSelectedPath( (FileObject) comboBoxModel.getSelectedItem() );
} else {
this.fileNameTextField.setText( null );
this.table.setSelectedPath( null );
this.locationCombo.setModel( new DefaultComboBoxModel() );
}
}
private void assertModel (Map<String, String> items, JComboBox combo, List<String> expected) {
ComboBoxModel model = combo.getModel();
assertEquals(expected.size() / 2, model.getSize());
for (int i = 0; i < model.getSize(); ++i) {
assertEquals(expected.get(i * 2), model.getElementAt(i));
assertEquals(expected.get(i * 2 + 1), items.get((String) model.getElementAt(i)));
}
}
@Override // DetailsDialogContent
public void setProperty(Property property) {
fProperty = (AbstractProperty) property;
@SuppressWarnings("unchecked")
ComboBoxModel<Object> model
= new DefaultComboBoxModel<>(((Selectable<Object>) fProperty).getPossibleValues().toArray());
valueComboBox.setModel(model);
Object value = fProperty.getValue();
valueComboBox.setSelectedItem(value);
valueComboBox.setRenderer(listCellRenderer);
}
private static ComboBoxModel<String> createPagesModel(int pages)
{
String[] pageNumbers = new String[pages];
for (int i = 0; i < pages; i++)
{
pageNumbers[i] = (i + 1) + " of " + pages;
}
return new DefaultComboBoxModel<>(pageNumbers);
}
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 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 ComboBoxModel createSelectorModel() {
HtmlModel model = HtmlModelFactory.getModel(HtmlVersion.HTML5);
Collection<String> tagNames = new ArrayList<>();
tagNames.add(null);
for (HtmlTag tag : model.getAllTags()) {
tagNames.add(tag.getName());
}
return new DefaultComboBoxModel(tagNames.toArray());
}
private ComboBoxModel createIdsModel() {
Collection<String> ids = new ArrayList<>();
ids.add(null);
FileObject selectedStyleSheet = (FileObject) styleSheetCB.getSelectedItem();
if (selectedStyleSheet != null) {
Collection<String> foundInFile = files2ids.get(selectedStyleSheet);
if (foundInFile != null) {
ids.addAll(foundInFile);
}
}
return new DefaultComboBoxModel(ids.toArray());
}
private boolean hasEmpty(ComboBoxModel model) {
int size = model.getSize();
for (int i=0; i<size; i++) {
if ("".equals(model.getElementAt(i))) {
return true;
}
}
return false;
}
private void updatePackageComboBox() {
SourceGroup sourceGroup = (SourceGroup)locationComboBox.getSelectedItem();
if (sourceGroup != null) {
ComboBoxModel model = PackageView.createListView(sourceGroup);
if (model.getSelectedItem()!= null && model.getSelectedItem().toString().startsWith("META-INF")
&& model.getSize() > 1) { // NOI18N
model.setSelectedItem(model.getElementAt(1));
}
packageComboBox.setModel(model);
}
}
private void updatePackageComboBox() {
SourceGroup sourceGroup = (SourceGroup) locationComboBox.getSelectedItem();
if (sourceGroup != null) {
ComboBoxModel model = PackageView.createListView(sourceGroup);
if (model.getSelectedItem() != null && model.getSelectedItem().toString().startsWith("META-INF")
&& model.getSize() > 1) { // NOI18N
model.setSelectedItem(model.getElementAt(1));
}
packageComboBox.setModel(model);
}
}
/**
* Sets the characters per inch.
*
* @param cpi
* the characters per inch.
*/
public void setCharsPerInch( final float cpi ) {
final Float cpiObj = new Float( cpi );
final ComboBoxModel model = cbCharsPerInch.getModel();
for ( int i = 0; i < model.getSize(); i++ ) {
if ( cpiObj.equals( model.getElementAt( i ) ) ) {
cbCharsPerInch.setSelectedIndex( i );
return;
}
}
throw new IllegalArgumentException( messages.getErrorString( "PlainTextExportDialog.ERROR_0002_NO_SUCH_CPI", String
.valueOf( cpi ) ) ); //$NON-NLS-1$ //$NON-NLS-2$
}
@SuppressWarnings("unchecked")
public ValueTypeCellEditor(final int valueType) {
super(new JComboBox<String>());
ComboBoxModel<String> model = new DefaultComboBoxModel<String>() {
private static final long serialVersionUID = 914764579359633239L;
private String[] valueTypes = new String[VALUE_TYPE_OPTIONS[valueType].length];
{
for (int i = 0; i < VALUE_TYPE_OPTIONS[valueType].length; i++) {
valueTypes[i] = Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(VALUE_TYPE_OPTIONS[valueType][i]);
}
}
@Override
public String getElementAt(int index) {
return valueTypes[index];
}
@Override
public int getSize() {
return valueTypes.length;
}
};
((JComboBox<?>) super.getComponent()).setEnabled(editValueTypes);
((JComboBox<String>) super.getComponent()).setModel(model);
}
@Override
protected void done()
{
try
{
ComboBoxModel<Object> model = new DefaultComboBoxModel<>(get());
model.setSelectedItem(null);
sheetBox.setModel(model);
}
catch (InterruptedException | ExecutionException ex)
{
Logging.errorPrint("could not load sheets", ex);
}
}
/**
* Maps {@code JComboBox.getModel()} through queue
*/
public ComboBoxModel<?> getModel() {
return (runMapping(new MapAction<ComboBoxModel<?>>("getModel") {
@Override
public ComboBoxModel<?> map() {
return ((JComboBox) getSource()).getModel();
}
}));
}