Skip to content

Commit 65cafca

Browse files
Update JComboBox choices during SwingChoiceWidget refresh
These changes ensure JComboBox items update during DynamicCallbacks
1 parent e68111f commit 65cafca

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/main/java/org/scijava/ui/swing/widget/SwingChoiceWidget.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ public void actionPerformed(final ActionEvent e) {
6767

6868
@Override
6969
public String getValue() {
70-
return comboBox.getSelectedItem().toString();
70+
if (comboBox.getItemCount() > 0)
71+
return comboBox.getSelectedItem().toString();
72+
else
73+
return null;
7174
}
7275

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

98101
@Override
99102
public void doRefresh() {
100-
final Object value = get().getValue();
101-
if (value.equals(comboBox.getSelectedItem())) return; // no change
102-
comboBox.setSelectedItem(value);
103+
final String[] choices = get().getChoices();
104+
105+
if (!areListsEqual(choices, comboBoxItems())) {
106+
comboBox.removeAllItems();
107+
for (int i=0; i<choices.length; i++)
108+
comboBox.addItem(choices[i]);
109+
} else {
110+
final Object value = get().getValue();
111+
if (value.equals(comboBox.getSelectedItem())) return;
112+
comboBox.setSelectedItem(value);
113+
}
114+
}
115+
116+
private boolean areListsEqual(String[] list1, String[] list2) {
117+
if (list1.length != list2.length)
118+
return false;
119+
120+
for (int i=0; i< list1.length; i++)
121+
if (!list1[i].equals(list2[i]))
122+
return false;
123+
124+
return true;
125+
}
126+
127+
private String[] comboBoxItems() {
128+
String[] comboItems = new String[comboBox.getItemCount()];
129+
for (int i=0; i <comboBox.getItemCount(); i++)
130+
comboItems[i] = comboBox.getItemAt(i);
131+
132+
return comboItems;
103133
}
104134
}

0 commit comments

Comments
 (0)