Skip to content

Commit 08fb795

Browse files
authored
Merge pull request #53 from karlduderstadt/master
2 parents 4093d4d + 9b88a14 commit 08fb795

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

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

+24-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import java.awt.event.ActionEvent;
3333
import java.awt.event.ActionListener;
34+
import java.util.Arrays;
3435

3536
import javax.swing.JComboBox;
3637
import javax.swing.JPanel;
@@ -67,7 +68,10 @@ public void actionPerformed(final ActionEvent e) {
6768

6869
@Override
6970
public String getValue() {
70-
return comboBox.getSelectedItem().toString();
71+
if (comboBox.getItemCount() > 0)
72+
return comboBox.getSelectedItem().toString();
73+
else
74+
return null;
7175
}
7276

7377
// -- WrapperPlugin methods --
@@ -97,8 +101,24 @@ public boolean supports(final WidgetModel model) {
97101

98102
@Override
99103
public void doRefresh() {
100-
final Object value = get().getValue();
101-
if (value.equals(comboBox.getSelectedItem())) return; // no change
102-
comboBox.setSelectedItem(value);
104+
final String[] choices = get().getChoices();
105+
106+
if (!Arrays.equals(choices, comboBoxItems())) {
107+
comboBox.removeAllItems();
108+
for (int i=0; i<choices.length; i++)
109+
comboBox.addItem(choices[i]);
110+
} else {
111+
final Object value = get().getValue();
112+
if (value.equals(comboBox.getSelectedItem())) return;
113+
comboBox.setSelectedItem(value);
114+
}
115+
}
116+
117+
private String[] comboBoxItems() {
118+
String[] comboItems = new String[comboBox.getItemCount()];
119+
for (int i=0; i <comboBox.getItemCount(); i++)
120+
comboItems[i] = comboBox.getItemAt(i);
121+
122+
return comboItems;
103123
}
104124
}

0 commit comments

Comments
 (0)