+ 12
How can I replace certain words in ComboBox model?
https://stackoverflow.com/q/51376283/10005675 I have created a ComboBox which contains some codename like (AJ2000). As soon as I check a Checkbox , it would show(MJ2000). Is there any method of doing this , since ComboBox model does not have replace kind of method.
3 Antworten
+ 8
I got the answer from stack overflow
MyItem[] items= new MyItem[] { new MyItem("test1",1),new MyItem("test2",2),new MyItem("test3",3),new MyItem("test4",4)};
DefaultComboBoxModel<MyItem> defaultComboBoxModel=new DefaultComboBoxModel<MyItem>(items);
JComboBox<MyItem> jComboBox=new JComboBox<MyItem>(defaultComboBoxModel);
defaultComboBoxModel.addElement(new MyItem("test5", 5));
// changing the combo box label by changing the main data array
for(MyItem item:items) {
if(item.getName().contains("test")) {
item.setName(item.getName().replaceAll("test","array item"));
}
}
// changing the combo box label by changing the Model Object value
for(int i=0;i<defaultComboBoxModel.getSize();i++) {
if(defaultComboBoxModel.getElementAt(i).getName().contains("test")) {
defaultComboBoxModel.getElementAt(i).setName(defaultComboBoxModel.getElementAt(i).getName().replaceAll("test", "other item"));
}
}
+ 5
Ezeome Ugo Depends on which IDE you are working.
0
how do you start coding