如何将JCombobox 内容排序?

解决方案 »

  1.   

    用这个构造函数试试看    public JComboBox(final Object items[]) {
            super();
            setModel(new DefaultComboBoxModel(items));
            init();
        }
      

  2.   

    1。继承JComboBox构造自己的类   
      2。在你构造的类中,定义PropertyChangeSupport的对象   
            PropertyChangeSupport   pcs   =   new   PropertyChangeSupport();   
      3。覆盖掉JComboBox的addItem(Object   obj)方法,在方法内调用激发器   public   void   addItem(Object   obj)   {   
                    super.addItem()   
        
                    Object   tempObj   =   this.obj;   
                    this.obj   =   obj;   
        
                    if   (this.obj   ==   obj)   {   
                            return;   
                    }   
                    PropertyChangeListener[]   ls   =   propertyChangeSupport.getPropertyChangeListeners();   
                    if   (   (   ls   ==   null   )   ||   (   ls.length   <=   0   )   )   {   
                            return;   
                    }   
                    propertyChangeSupport.firePropertyChange("obj",tempObj,obj);   
            }  
      
        
      4。在定义你构造的类的对象后,为此对象加载PropertyChangelistener   
        
      这样在你每次进行输入后确认的时候,肯定会调用addItem(Object   obj)方法(已经被覆盖的)。这样就可以激发PropertyChangeListener了      
      ps:上面的代码没有上机测试,不敢保证准确,楼下的指正,不胜感激
      

  3.   

    把你的JCombobox的model排下序就好了
      

  4.   

    From 5 "把你的JCombobox的model排下序就好了"up.
      

  5.   


    public class TestT extends JFrame {



    public TestT() {

    JComboBox com =new JComboBox();
    com.setModel(new SortDefaultComboBoxModel());

    com.addItem("cc");
    com.addItem("aa");
    com.addItem("bb");

    com.insertItemAt("ac", 0);

    this.getContentPane().add(com, BorderLayout.CENTER);
    this.setSize(200, 50);
    this.setLocationRelativeTo(null);
    this.setVisible(true);
    }


    class SortDefaultComboBoxModel extends DefaultComboBoxModel implements java.util.Comparator<String>{

    Vector<String> objects = new Vector<String>();
        Object selectedObject;

        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;
        }
        
        public int getSize() {
            return objects.size();
        }
        
        public Object getElementAt(int index) {
            if ( index >= 0 && index < objects.size() )
                return objects.elementAt(index);
            else
                return null;
        }
        
        public int getIndexOf(Object anObject) {
            return objects.indexOf(anObject);
        }
        
        
        public void addElement(Object anObject) {
            objects.addElement((String)anObject);
            fireIntervalAdded(this,objects.size()-1, objects.size()-1);
            if ( objects.size() == 1 && selectedObject == null && anObject != null ) {
                setSelectedItem( anObject );
            }
            java.util.Collections.sort(objects);
        }
        
        public void insertElementAt(Object anObject,int index) {
            objects.insertElementAt((String)anObject,index);
            fireIntervalAdded(this, index, index);
            java.util.Collections.sort(objects);
        }     public void removeElement(Object anObject) {
            int index = objects.indexOf(anObject);
            if ( index != -1 ) {
                removeElementAt(index);
            }
        }
    public int compare(String o1, String o2) {
    return o1.compareTo(o2);
    }

    }

    public static void main(String[] args) {
    new TestT();
    }
    }
      

  6.   

    JComboBox com =new JComboBox();Collection collection = new Vector();
    collection.add(obj1);
    collection.add(obj2);
    collection.add(obj3);
    collection.add(obj4);
     .    .    .
     .    .    .
     .    .    .
    Collections.sort(collection);for(Iterator iter = collection.iterator();iter.hasNext();){
      Object ojb = iter.next();
      com.addItem(ojb);
    }