I’m using my code in this post to make changes to the drop down menu component’s DataSetComboBoxModel
and NewPMIComboBoxRenderer
classes, however it’s always had the issue where the component will show <Select One>
when the DataSetComboBoxModel
is replaced. I can work around this by getting the selected item prior to replacing the model, then after replacing it, writing it back to it, but I wonder how I can do this within the class itself?
The section of interest is this:
class NewPMIComboBoxModel(DataSetComboBoxModel):
def __init__(self, target):
DataSetComboBoxModel.__init__(self, target, target.data)
self.target = target
As soon as I run the below, the drop down selection will be lost and <Select One>
will display:
RC = system.gui.getParentWindow(event).rootContainer
PMIComboBox = RC.getComponent("Dropdown")
PMIComboBox.setModel(NewPMIComboBoxModel(PMIComboBox))
How can I maintain the selection?
Cheers
In your subclass of DataSetComboBoxModel
, call self.setSelectedItem()
within your __init__()
method, so that it is already “selected” when the model object replacement takes effect.
1 Like
Hmm, I thought I’d tried this already but I have it working now. Actually I was calling self.target.setSelectedItem()
instead.
Thanks!
Strangely though, when I run print self.getSelectedItem()
, it still returns <Select One>
, but the component itself shows the actual selected item… print target.getSelectedItem()
alternatively shows the actual selected item.
Running either self.setSelectedItem(self.getSelectedItem())
or self.setSelectedItem(target.getSelectedItem())
both ensure the item is selected.
Good timing. Time for me to crash. (:
1 Like
FWIW, I would use:
try:
self.selectedItem = target.model.selectedItem
except:
pass
I always use netbeans syntax for readability, and I tend to be paranoid about unexpected failures.
1 Like
Noted and good point RE try/except. Thanks again!
1 Like