Ensure new model uses the selected item from the old model

This commit is contained in:
SiboVG 2024-08-12 17:20:06 +02:00
parent 3f47259bf5
commit 1f6df48d97
2 changed files with 12 additions and 6 deletions

View File

@ -35,6 +35,7 @@ import info.openrocket.core.preferences.ApplicationPreferences;
import info.openrocket.core.unit.Unit;
import info.openrocket.core.util.Utils;
import info.openrocket.swing.gui.widgets.GroupableAndSearchableComboBox;
import net.miginfocom.swing.MigLayout;
import info.openrocket.swing.gui.components.DescriptionArea;
import info.openrocket.swing.gui.components.UnitSelector;
@ -101,7 +102,7 @@ public class SimulationPlotPanel extends JPanel {
private JComboBox<PlotConfiguration> configurationSelector;
private JComboBox<FlightDataType> domainTypeSelector;
private GroupableAndSearchableComboBox<FlightDataTypeGroup, FlightDataType> domainTypeSelector;
private UnitSelector domainUnitSelector;
private JPanel typeSelectorPanel;
@ -487,7 +488,7 @@ public class SimulationPlotPanel extends JPanel {
private final String[] POSITIONS = { AUTO_NAME, LEFT_NAME, RIGHT_NAME };
private final int index;
private final JComboBox<FlightDataType> typeSelector;
private final GroupableAndSearchableComboBox<FlightDataTypeGroup, FlightDataType> typeSelector;
private UnitSelector unitSelector;
private JComboBox<String> axisSelector;

View File

@ -152,6 +152,9 @@ public class GroupableAndSearchableComboBox<G extends Group, T extends Groupable
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value != null) {
label.setText(getDisplayString((T) value));
} else {
// Handle the case when no item is selected
label.setText("");
}
return label;
}
@ -162,9 +165,11 @@ public class GroupableAndSearchableComboBox<G extends Group, T extends Groupable
this.itemGroupMap = new LinkedHashMap<>(itemGroupMap); // Create a copy to avoid external modifications
this.allItems = extractItemsFromMap(itemGroupMap);
// Update the existing model instead of creating a new one
// Update the model
if (getModel() instanceof DefaultComboBoxModel<T>) {
T selectedItem = (T) getModel().getSelectedItem();
ComboBoxModel<T> model = new DefaultComboBoxModel<>(new Vector<>(allItems));
model.setSelectedItem(selectedItem);
setModel(model);
setupModelListener(model);
}
@ -364,7 +369,7 @@ public class GroupableAndSearchableComboBox<G extends Group, T extends Groupable
public void setSelectedItem(Object anObject) {
// Hide the popups after selection
hidePopups();
super.setSelectedItem(anObject);
getModel().setSelectedItem(anObject);
}
private void filter(String text) {
@ -438,7 +443,7 @@ public class GroupableAndSearchableComboBox<G extends Group, T extends Groupable
}
private void setupModelListener(ComboBoxModel<T> model) {
if (model == null) {
/*if (model == null) {
return;
}
model.addListDataListener(this);
@ -447,7 +452,7 @@ public class GroupableAndSearchableComboBox<G extends Group, T extends Groupable
public void actionPerformed(ActionEvent e) {
model.setSelectedItem(GroupableAndSearchableComboBox.this.getSelectedItem());
}
});
});*/
}
private void setupSearchFieldListeners() {