Skip to content

Commit

Permalink
Update JComboBox choices during SwingChoiceWidget refresh
Browse files Browse the repository at this point in the history
These changes ensure JComboBox items update during DynamicCallbacks
  • Loading branch information
karlduderstadt committed Oct 21, 2020
1 parent e68111f commit 65cafca
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/main/java/org/scijava/ui/swing/widget/SwingChoiceWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ public void actionPerformed(final ActionEvent e) {

@Override
public String getValue() {
return comboBox.getSelectedItem().toString();
if (comboBox.getItemCount() > 0)
return comboBox.getSelectedItem().toString();
else
return null;
}

// -- WrapperPlugin methods --
Expand Down Expand Up @@ -97,8 +100,35 @@ public boolean supports(final WidgetModel model) {

@Override
public void doRefresh() {
final Object value = get().getValue();
if (value.equals(comboBox.getSelectedItem())) return; // no change
comboBox.setSelectedItem(value);
final String[] choices = get().getChoices();

if (!areListsEqual(choices, comboBoxItems())) {
comboBox.removeAllItems();
for (int i=0; i<choices.length; i++)
comboBox.addItem(choices[i]);
} else {
final Object value = get().getValue();
if (value.equals(comboBox.getSelectedItem())) return;
comboBox.setSelectedItem(value);
}
}

private boolean areListsEqual(String[] list1, String[] list2) {
if (list1.length != list2.length)
return false;

for (int i=0; i< list1.length; i++)
if (!list1[i].equals(list2[i]))
return false;

return true;
}

private String[] comboBoxItems() {
String[] comboItems = new String[comboBox.getItemCount()];
for (int i=0; i <comboBox.getItemCount(); i++)
comboItems[i] = comboBox.getItemAt(i);

return comboItems;
}
}

0 comments on commit 65cafca

Please sign in to comment.