-
Enhancement
-
Resolution: Won't Fix
-
P4
-
None
-
1.4.0
-
x86
-
windows_nt
Developers who wish to represent their own data storage of ComboBox items within a
custom ComboBoxModel may face many incompatibilities if they implement the
ComboBoxModel interfaces directly. Part of the reason is that ListDataModel.contentsChanged
event firing in DefaultComboBoxModel.setSelectedItem is important for the proper
functioning of events that originate from the JComboBox. Implementation of custom combo
box models will usually have to copy the selection functionality from DefaultComboBoxModel.
We propose an AbstractComboBoxModel which implements ComboBoxModel
and takes care of the semantics of the current item selection. The implementation
of the ComboBoxModel methods are taken from a refactoring of DefaultComboBoxModel.
public abstract class AbstractComboBoxModel extends AbstractListModel implements ComboBoxModel {
/**
* The current selected object.
*/
protected Object selectedObject;
// implementation of javax.swing.ComboBoxModel
/**
* Set the value of the selected item. The selected item may be null.
* <p>
* @param anObject The combo box value or null for no selection.
*/
public void setSelectedItem(Object anObject) {
if ((selectedObject != null && !selectedObject.equals( anObject )) ||
selectedObject == null && anObject != null) {
selectedObject = anObject;
fireContentsChanged(this, -1, -1);
}
}
public Object getSelectedItem() {
return selectedObject;
}
}
The DefaultComboBoxModel can be written to extend AbstractComboBoxModel:
public class DefaultComboBoxModel extends AbstractComboBoxModel implements MutableComboBoxModel {
....
}
Custom combo box models can inherit from AbstractComboBoxModel and can rely on the correct
selection notification to occur.
For example, an immutable combo box that uses the java.util.List as the storage
implementation would just have to implement the remaining ListModel methods:
public class MyComboBoxModel extends AbstractComboBoxModel {
private List objects;
public MyComboBoxModel() {
objects = new ArrayList();
objects.add(new FooItem());
....
}
// javax.swing.ListModel implementation
public Object getElementAt(int index) {
return objects.get(index);
}
public int getSize() {
return objects.size();
}
}
The advantage of extending this class is that the implementor can focus on the data storage and retrieval
and will not have to worry about managing the selection.
A custom mutable combo box model would have to follow the example of the DefaultComboBoxModel and
implement the MutableComboBoxModel methods and ensure that the correct ListDataListener methods
are fired.
custom ComboBoxModel may face many incompatibilities if they implement the
ComboBoxModel interfaces directly. Part of the reason is that ListDataModel.contentsChanged
event firing in DefaultComboBoxModel.setSelectedItem is important for the proper
functioning of events that originate from the JComboBox. Implementation of custom combo
box models will usually have to copy the selection functionality from DefaultComboBoxModel.
We propose an AbstractComboBoxModel which implements ComboBoxModel
and takes care of the semantics of the current item selection. The implementation
of the ComboBoxModel methods are taken from a refactoring of DefaultComboBoxModel.
public abstract class AbstractComboBoxModel extends AbstractListModel implements ComboBoxModel {
/**
* The current selected object.
*/
protected Object selectedObject;
// implementation of javax.swing.ComboBoxModel
/**
* Set the value of the selected item. The selected item may be null.
* <p>
* @param anObject The combo box value or null for no selection.
*/
public void setSelectedItem(Object anObject) {
if ((selectedObject != null && !selectedObject.equals( anObject )) ||
selectedObject == null && anObject != null) {
selectedObject = anObject;
fireContentsChanged(this, -1, -1);
}
}
public Object getSelectedItem() {
return selectedObject;
}
}
The DefaultComboBoxModel can be written to extend AbstractComboBoxModel:
public class DefaultComboBoxModel extends AbstractComboBoxModel implements MutableComboBoxModel {
....
}
Custom combo box models can inherit from AbstractComboBoxModel and can rely on the correct
selection notification to occur.
For example, an immutable combo box that uses the java.util.List as the storage
implementation would just have to implement the remaining ListModel methods:
public class MyComboBoxModel extends AbstractComboBoxModel {
private List objects;
public MyComboBoxModel() {
objects = new ArrayList();
objects.add(new FooItem());
....
}
// javax.swing.ListModel implementation
public Object getElementAt(int index) {
return objects.get(index);
}
public int getSize() {
return objects.size();
}
}
The advantage of extending this class is that the implementor can focus on the data storage and retrieval
and will not have to worry about managing the selection.
A custom mutable combo box model would have to follow the example of the DefaultComboBoxModel and
implement the MutableComboBoxModel methods and ensure that the correct ListDataListener methods
are fired.