Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with userdefined combobox selection

Status
Not open for further replies.

kohinoor2007

Programmer
Mar 21, 2007
69
0
0
DE

Hi guys,

Have an "AutoSearchComboBox" as follows for searching the combolist fast.Have some problems with the selection in the AutoSearchComboBox.



public class AutoSearchComboBox extends JComboBox {

/**
*
*/
private static final long serialVersionUID = 1L;
public AutoSearchComboBox(){

setEditable(true);
setModel(new AutoSearchComboBoxModel());
setEditor(new MyComboBoxEditor());
}

public class AutoSearchComboBoxModel extends AbstractListModel
implements MutableComboBoxModel{

/**
*
*/
private static final long serialVersionUID = 1L;
private Vector items,filteredItems;
private Filter filter;
private Object selectedItem;

public AutoSearchComboBoxModel(){

this.items = new Vector();
this.filteredItems = new Vector();
this.filter = null;
updateFilteredItems();
}
public void addElement(Object obj) {

items.add(obj);
updateFilteredItems();
}

public void insertElementAt(Object obj, int index) {}

public void removeElement(Object obj) {

items.remove(obj);
updateFilteredItems();
}

public void removeElementAt(int index) {

items.remove(index);
updateFilteredItems();
}

public Object getSelectedItem() {

return selectedItem;
}

public void setSelectedItem(Object anItem) {

if ((selectedItem==null) && (anItem==null))
return;
if ((selectedItem != null) && (selectedItem.equals(anItem)))
return;
if(anItem!= null && anItem.equals(selectedItem))
return;

selectedItem = anItem;
fireContentsChanged(this, -1,-1);

}

public Object getElementAt(int index) {

return filteredItems.get(index);
}

public int getSize() {

return filteredItems.size();
}

public void setFilter(Filter filter){

this.filter = filter;
updateFilteredItems();
}

public void updateFilteredItems(){

fireIntervalRemoved(this,0,filteredItems.size());
filteredItems.clear();
if(filter==null)
filteredItems.addAll(items);
else
{
for(Iterator iter = items.iterator(); iter.hasNext();){

Object item = iter.next();
if (filter.accept(item))
filteredItems.add(item);

}
}
fireIntervalAdded(this,0, filteredItems.size());
}

}
public static interface Filter{
public boolean accept(Object obj);
}
class FilterItems implements Filter{

private String prefix;

public FilterItems(String prefix){
this.prefix = prefix;
}

public boolean accept(Object obj){

return doesStartsWithThePrefix(obj.toString(),prefix);
}
private boolean doesStartsWithThePrefix(String str1,String str2){

return str1.toUpperCase().startsWith(str2.toUpperCase());
}
}

public class MyComboBoxEditor implements ComboBoxEditor,DocumentListener{

private JTextField text;
private volatile boolean filtering = false;
private volatile boolean setting = false;
public MyComboBoxEditor(){

text = new JTextField(15);
text.getDocument().addDocumentListener(this);
}

public void addActionListener(ActionListener l) {
// TODO Auto-generated method stub

}
public void removeActionListener(ActionListener l) {
// TODO Auto-generated method stub
}

public Component getEditorComponent() {

return text;
}

public void setItem(Object anObject) {

if(filtering)
return;
setting = true;
String newText = (anObject==null)? "":anObject.toString();
text.setText(newText);
setting = false;
}

public Object getItem() {

return text.getText();
}

public void selectAll() {

text.selectAll();
}

public void changedUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
}

public void insertUpdate(DocumentEvent e) {

handleChange();
}

public void removeUpdate(DocumentEvent e) {

handleChange();

}
public void handleChange(){

if(setting)
return;

filtering = true;
Filter filter = null;
if(text.getText().length()>0)
filter = new FilterItems(text.getText());

((AutoSearchComboBoxModel)getModel()).setFilter(filter);

setPopupVisible(false);
if (getModel().getSize()>0)
setPopupVisible(true);
filtering = false;
}

}

}







Iam creating the combo boxes as follows from another source file.

private AutoSearchComboBox indexTagCombo,indexValueCombo


//The following function is called for loading the comboBoxes.
private void loadInitialIndexTagValues()
{

indexTagCombo.removeAllItems();
//Getting values to be fiiled in the comboBox.
Set tags = this.model.getAvailableIndexTags();
// Iterating over the elements in the set
Iterator tagIter = tags.iterator();
while (tagIter.hasNext()) {

indexTagCombo.addItem(tagIter.next());
}

//Loading index values.
String currTag = (String)indexTagCombo.getSelectedItem(); //Selection problem 1

The statement above returns me null & the follwing code doesnt fill the other comboBox.


Set values = model.getAllValues(currTag);
indexValueCombo.removeAllItems();
Iterator valIter = values.iterator();
while (valIter.hasNext()) {
indexValueCombo.addItem(valIter.next());

}
}

Have added an "itemListener" to the indexTagCombo as follows:
indexTagCombo.addItemListener(new ItemListener(){

public void itemStateChanged(ItemEvent e)
{
indexTagSelection(e);
}
});


private void indexTagSelection(ItemEvent e) {

if (e.getStateChange() == ItemEvent.SELECTED)
{
//The "getStateChange" is never ItemEvent.SELECTED,so that it never gets in to this loop.//Another problem
//code goes Here
}
}


There is some problem with the defined "AutoSearchComboBox" with selection.When I change the privately defined "AutoSearchComboBox" to JComboBox and make it editable,all the selection problems are gone.

In short all the "getSelectedItem" from the "AutoSearchComboBox" and the "itemstate" events defined are not going to the "ItemEvent.SELECTED"

What should I fix in the "AutoSearchComboBox" so that the selections work.

Any help is greatly appreciated.

Thanks
P
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top