Skip to content

Commit

Permalink
Merge pull request #53 from karlduderstadt/master
Browse files Browse the repository at this point in the history
  • Loading branch information
imagejan authored Jan 10, 2021
2 parents 4093d4d + 9b88a14 commit 08fb795
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 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 @@ -31,6 +31,7 @@

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JComboBox;
import javax.swing.JPanel;
Expand Down Expand Up @@ -67,7 +68,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 +101,24 @@ 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 (!Arrays.equals(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 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 08fb795

Please sign in to comment.