Skip to content

SwingObjectWidget: set values by string comparison #87

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 18 additions & 3 deletions src/main/java/org/scijava/ui/swing/widget/SwingObjectWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,23 @@ public boolean supports(final WidgetModel model) {
@Override
public void doRefresh() {
final Object value = get().getValue();
if (value == comboBox.getSelectedItem()) return; // no change
comboBox.setSelectedItem(value);
if (comboBox.getSelectedItem().toString().equals(nameOf(value))) return; // no change
updateComboBox(comboBox, value);
}

private void updateComboBox(final JComboBox<Object> comboBox, final Object value) {
for (int i = 0; i < comboBox.getModel().getSize(); i++)
{
if (comboBox.getItemAt(i).toString().equals(nameOf(value)))
{
comboBox.setSelectedIndex(i);
break;
}
}
}

private String nameOf(final Object value) {
return value == null ? "" : context().service(ObjectService.class).getName(value);
}

private class NamedObjectCellRenderer implements ListCellRenderer<Object> {
Expand All @@ -120,7 +135,7 @@ private class NamedObjectCellRenderer implements ListCellRenderer<Object> {
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
renderer.setText(value == null ? "" : context().service(ObjectService.class).getName(value));
renderer.setText(nameOf(value));
renderer.setToolTipText(value == null ? null : value.toString());
return renderer;
}
Expand Down