Skip to content

Update JComboBox choices during SwingChoiceWidget refresh #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}