本文整理汇总了Java中javax.swing.MutableComboBoxModel.getElementAt方法的典型用法代码示例。如果您正苦于以下问题:Java MutableComboBoxModel.getElementAt方法的具体用法?Java MutableComboBoxModel.getElementAt怎么用?Java MutableComboBoxModel.getElementAt使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.MutableComboBoxModel
的用法示例。
在下文中一共展示了MutableComboBoxModel.getElementAt方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setSelectedBean
import javax.swing.MutableComboBoxModel; //导入方法依赖的package包/类
/**
* Sets the bean on this editor.
* If an editor of the same class is found, it is selected an modified to match
* If no bean of this type is found, it is added to the list
*
* @param bean the bean
*/
public void setSelectedBean(final IBean bean) {
final MutableComboBoxModel<IBean> model = (MutableComboBoxModel<IBean>) selector.getModel();
final DefaultComboBoxModel<IBean> newModel = new DefaultComboBoxModel<>();
boolean found = false;
for (int i = 0; i < model.getSize(); i++) {
final IBean candidate = model.getElementAt(i);
if (candidate.equals(bean)) {
found = true;
newModel.addElement(bean);
} else {
newModel.addElement(candidate);
}
}
if (found) {
selector.setModel(newModel);
} else {
model.addElement(bean);
}
selector.setSelectedItem(bean);
updateView();
}
示例2: insertIntoCombo
import javax.swing.MutableComboBoxModel; //导入方法依赖的package包/类
/**
* Insert the given item into the combo box, and set it as first selected
* item. If the item already exists, it is removed, so there are no
* duplicates.
* @param combo
* @param item the item to insert. if it's null, then nothing is inserted
*/
public static void insertIntoCombo(JComboBox combo, Object item) {
if(item == null) {
return;
}
MutableComboBoxModel model = (MutableComboBoxModel) combo.getModel();
if (model.getSize() == 0) {
model.insertElementAt(item, 0);
return;
}
Object o = model.getElementAt(0);
if (o.equals(item)) {
return;
}
model.removeElement(item);
model.insertElementAt(item, 0);
combo.setSelectedIndex(0);
}