下面列出了javax.swing.DefaultComboBoxModel#removeElementAt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void handleAggregationChanged(boolean updateSecondary) {
if (updateSecondary) {
DefaultComboBoxModel model = (DefaultComboBoxModel)secondCombo.getModel();
while (model.getSize() > 1) model.removeElementAt(1);
// if (!Aggregation.CLASS.equals(firstCombo.getSelectedItem()) &&
// !Aggregation.CLASS_MESSAGE.equals(firstCombo.getSelectedItem()))
// model.addElement(Aggregation.CLASS);
//
// if (!Aggregation.MESSAGE.equals(firstCombo.getSelectedItem()) &&
// !Aggregation.CLASS_MESSAGE.equals(firstCombo.getSelectedItem()))
// model.addElement(Aggregation.MESSAGE);
//
// if (!Aggregation.CLASS.equals(firstCombo.getSelectedItem()) &&
// !Aggregation.MESSAGE.equals(firstCombo.getSelectedItem()) &&
// !Aggregation.CLASS_MESSAGE.equals(firstCombo.getSelectedItem()))
// model.addElement(Aggregation.CLASS_MESSAGE);
//
// if (!Aggregation.THREAD.equals(firstCombo.getSelectedItem()))
// model.addElement(Aggregation.THREAD);
}
updateButton.setEnabled(lastPrimary != firstCombo.getSelectedItem() ||
lastPhase != secondChoice.isSelected());
}
private void handleAggregationChanged(boolean updateSecondary) {
if (updateSecondary) {
DefaultComboBoxModel model = (DefaultComboBoxModel)secondCombo.getModel();
while (model.getSize() > 1) model.removeElementAt(1);
if (!Aggregation.ADDRESS.equals(firstCombo.getSelectedItem()) &&
!Aggregation.PORT.equals(firstCombo.getSelectedItem()) &&
!Aggregation.ADDRESS_PORT.equals(firstCombo.getSelectedItem()))
model.addElement(Aggregation.ADDRESS_PORT);
if (!Aggregation.ADDRESS.equals(firstCombo.getSelectedItem()) &&
!Aggregation.ADDRESS_PORT.equals(firstCombo.getSelectedItem()))
model.addElement(Aggregation.ADDRESS);
if (!Aggregation.PORT.equals(firstCombo.getSelectedItem()) &&
!Aggregation.ADDRESS_PORT.equals(firstCombo.getSelectedItem()))
model.addElement(Aggregation.PORT);
if (!Aggregation.THREAD.equals(firstCombo.getSelectedItem()))
model.addElement(Aggregation.THREAD);
}
updateButton.setEnabled(lastPrimary != firstCombo.getSelectedItem() ||
lastSecondary != secondCombo.getSelectedItem());
}
/**
* Checks if the item is in the combobox. If yes, then it is moved to the beginning,
* otherwise, the item is added as first.
* @param <T> the element type
* @param combobox the combobox
* @param item the item
*/
public static <T> void addFirstItem(JComboBox<T> combobox, T item) {
int idx = -1;
DefaultComboBoxModel<T> model = (DefaultComboBoxModel<T>)combobox.getModel();
for (int i = 0; i < model.getSize(); i++) {
T t = model.getElementAt(i);
if (Objects.equals(t, item)) {
idx = i;
break;
}
}
model.insertElementAt(item, 0);
if (idx >= 0) {
model.removeElementAt(idx + 1);
}
}
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);
}
}
private void removeCustomValue() {
for( int i=0; i<getItemCount(); i++ ) {
ColorValue cv = ( ColorValue ) getItemAt( i );
if( cv.isCustom ) {
DefaultComboBoxModel model = ( DefaultComboBoxModel ) getModel();
model.removeElementAt( i );
return;
}
}
}
private ComboListElement addPath(String path) {
DefaultComboBoxModel model = (DefaultComboBoxModel)fileMRU.getModel();
ComboListElement newPath = new StringComboListElement(path);
int index = model.getIndexOf(newPath);
if (index == -1) {
model.insertElementAt(newPath, 1);
}
if (model.getSize() > itemCountLimit + 3) {
model.removeElementAt(model.getSize() - 3);
}
return newPath;
}
private void handleAggregationChanged(boolean updateSecondary) {
if (updateSecondary) {
DefaultComboBoxModel model = (DefaultComboBoxModel)secondCombo.getModel();
while (model.getSize() > 1) model.removeElementAt(1);
if (!Aggregation.CLASS.equals(firstCombo.getSelectedItem()) &&
!Aggregation.CLASS_MESSAGE.equals(firstCombo.getSelectedItem()))
model.addElement(Aggregation.CLASS);
if (!Aggregation.MESSAGE.equals(firstCombo.getSelectedItem()) &&
!Aggregation.CLASS_MESSAGE.equals(firstCombo.getSelectedItem()))
model.addElement(Aggregation.MESSAGE);
if (!Aggregation.CLASS.equals(firstCombo.getSelectedItem()) &&
!Aggregation.MESSAGE.equals(firstCombo.getSelectedItem()) &&
!Aggregation.CLASS_MESSAGE.equals(firstCombo.getSelectedItem()))
model.addElement(Aggregation.CLASS_MESSAGE);
if (!Aggregation.THREAD.equals(firstCombo.getSelectedItem()))
model.addElement(Aggregation.THREAD);
}
updateButton.setEnabled(lastMode != modeCombo.getSelectedIndex() ||
lastPrimary != firstCombo.getSelectedItem() ||
lastSecondary != secondCombo.getSelectedItem());
}
private void handleAggregationChanged(boolean updateSecondary) {
if (updateSecondary) {
int sel = secondCombo.getSelectedIndex();
DefaultComboBoxModel model = (DefaultComboBoxModel)secondCombo.getModel();
while (model.getSize() > 1) model.removeElementAt(1);
if (!Aggregation.CLASS.equals(firstCombo.getSelectedItem()) &&
!Aggregation.OBJECT.equals(firstCombo.getSelectedItem()))
model.addElement(Aggregation.CLASS);
if (!Aggregation.CLASS.equals(firstCombo.getSelectedItem()) &&
!Aggregation.OBJECT.equals(firstCombo.getSelectedItem()))
model.addElement(Aggregation.OBJECT);
if (!Aggregation.THREAD_BLOCKED.equals(firstCombo.getSelectedItem()))
model.addElement(Aggregation.THREAD_BLOCKED);
if (!Aggregation.THREAD_BLOCKING.equals(firstCombo.getSelectedItem()))
model.addElement(Aggregation.THREAD_BLOCKING);
secondCombo.setSelectedIndex(sel < secondCombo.getItemCount() ? sel : 0);
}
updateButton.setEnabled(lastMode != modeCombo.getSelectedIndex() ||
lastPrimary != firstCombo.getSelectedItem() ||
lastSecondary != secondCombo.getSelectedItem());
}
public static void savePrefs(Preferences prefs, String prefKey, JComboBox combo, String newValidValue) {
if (newValidValue == null) {
return;
}
DefaultComboBoxModel comboModel = (DefaultComboBoxModel) combo.getModel();
int existingIndex = comboModel.getIndexOf(newValidValue);
if (existingIndex >= 0) {
comboModel.removeElementAt(existingIndex);
}
comboModel.insertElementAt(newValidValue, 0);
combo.setSelectedIndex(0);
StringBuilder entries = new StringBuilder();
int size = Math.min(comboModel.getSize(), 20);
for (int i = 0; i < size; i++) {
entries.append(comboModel.getElementAt(i));
if (i != size - 1) {
entries.append(DELIMITER);
}
}
while (entries.length() > Preferences.MAX_VALUE_LENGTH) {
int lastIndex = entries.lastIndexOf(DELIMITER);
if (lastIndex == -1) {
break;
} else {
entries.delete(lastIndex, entries.length());
}
}
prefs.put(prefKey, entries.toString());
try {
prefs.flush();
} catch (Throwable e) {
e.printStackTrace();
}
}