0

我有一个 Jcombobox 显示项目列表 - 红色、蓝色、黄色和绿色,现在我希望 jcombox 在字符串 x 超过 300 个字符时仅显示红色和蓝色,如果字符串 x 更多则仅显示黄色和绿色超过 500 个字符。我怎么能在 java 中做到这一点。谢谢

4

2 回答 2

1

您只需要一个 if 条件,如果 x 大于 300 且小于 500,则用红色和蓝色项目填充 jcombobox,否则超过 500,然后用绿色和黄色项目填充。 http://www.exampledepot.com/egs/javax.swing/combobox_CbAddItems.html 这是一个简单的例子,可以在 java 中添加和删除 jcombo 框。希望这有帮助。

于 2012-10-14T19:44:49.807 回答
0

您可以使用允许您实时过滤结果的代理组合框模型。代理模型将使用您现有的模型并过滤结果,以便组合框仅看到过滤后的结果...

这是ListModel我用于此目的的 a 的实现...

public class FilteredListModel extends AbstractListModel implements ListDataListener {

    private ListModel peer;
    private List<Integer> lstFilteredIndicies;
    private IFilterable filter;

    public FilteredListModel() {
        lstFilteredIndicies = new ArrayList<Integer>(25);
    }

    public FilteredListModel(ListModel model) {
        this();
        setModel(model);
    }

    public FilteredListModel(ListModel model, IFilterable filter) {
        this();

        setModel(model);
        setFilter(filter);
    }

    public void setModel(ListModel parent) {
        if (peer == null || !peer.equals(parent)) {
            if (peer != null) {
                fireIntervalRemoved(this, 0, peer.getSize() - 1);
                peer.removeListDataListener(this);
            }
            peer = parent;
            lstFilteredIndicies.clear();
            if (peer != null) {
                peer.addListDataListener(this);
            }
            filterModel();
        }
    }

    public ListModel getModel() {
        return peer;
    }

    public int getSize() {
        IFilterable filter = getFilter();
        return filter == null ? getModel() == null ? 0 : getModel().getSize() : lstFilteredIndicies.size();
    }

    public Object getElementAt(int index) {
        IFilterable filter = getFilter();
        ListModel model = getModel();
        return filter == null ? model == null ? null : model.getElementAt(index) : model.getElementAt(lstFilteredIndicies.get(index));
    }

    public int indexOf(Object value) {
        int index = -1;
        for (int loop = 0; loop < getSize(); loop++) {
            Object at = getElementAt(loop);
            if (at == value) {
                index = loop;
                break;
            }
        }
        return index;
    }

    public void intervalAdded(ListDataEvent e) {
        IFilterable filter = getFilter();
        ListModel model = getModel();

        if (model != null) {
            if (filter != null) {
                int startIndex = Math.min(e.getIndex0(), e.getIndex1());
                int endIndex = Math.max(e.getIndex0(), e.getIndex1());
                for (int index = startIndex; index <= endIndex; index++) {
                    Object value = model.getElementAt(index);
                    if (filter.include(value)) {
                        lstFilteredIndicies.add(index);
                        int modelIndex = lstFilteredIndicies.indexOf(index);
                        fireIntervalAdded(this, modelIndex, modelIndex);
                    }
                }
            } else {
                fireIntervalAdded(this, e.getIndex0(), e.getIndex1());
            }
        }
    }

    public void intervalRemoved(ListDataEvent e) {
        IFilterable filter = getFilter();
        ListModel model = getModel();

        if (model != null) {
            if (filter != null) {
                int startIndex = Math.min(e.getIndex0(), e.getIndex1());
                int endIndex = Math.max(e.getIndex0(), e.getIndex1());
                for (int index = startIndex; index <= endIndex; index++) {
                    if (lstFilteredIndicies.contains(index)) {
                        int filterIndex = lstFilteredIndicies.indexOf(index);
                        lstFilteredIndicies.remove(filterIndex);
                        fireIntervalRemoved(this, filterIndex, filterIndex);
                    }
                }
            } else {
                fireIntervalRemoved(this, e.getIndex0(), e.getIndex1());
            }
        }
    }

    public void contentsChanged(ListDataEvent e) {
        filterModel();
    }

    public void setFilter(IFilterable value) {
        if (filter == null || !filter.equals(value)) {
            filter = value;
            if (getModel() != null) {
                fireIntervalRemoved(this, 0, getModel().getSize() - 1);
            }
            filterModel();
        }
    }

    public IFilterable getFilter() {
        return filter;
    }

    protected void filterModel() {
        if (getSize() > 0) {
            fireIntervalRemoved(this, 0, getSize() - 1);
        }
        lstFilteredIndicies.clear();

        IFilterable filter = getFilter();
        ListModel model = getModel();
        if (filter != null && model != null) {
            for (int index = 0; index < model.getSize(); index++) {
                Object value = model.getElementAt(index);
                if (filter.include(value)) {
                    lstFilteredIndicies.add(index);
                    fireIntervalAdded(this, getSize() - 1, getSize() - 1);
                }
            }
        }
    }

    public void updateFilter() {

        IFilterable filter = getFilter();
        ListModel model = getModel();

        if (filter != null && model != null) {
            for (int index = 0; index < model.getSize(); index++) {
                Object value = model.getElementAt(index);
                if (filter.include(value)) {
                    if (!lstFilteredIndicies.contains(index)) {
                        lstFilteredIndicies.add(index);
                        fireIntervalAdded(this, getSize() - 1, getSize() - 1);
                    }
                } else if (lstFilteredIndicies.contains(index)) {
                    int oldIndex = lstFilteredIndicies.indexOf(index);
                    lstFilteredIndicies.remove(oldIndex);
                    fireIntervalRemoved(this, oldIndex, oldIndex);
                }
            }
        }
    }
}

可过滤接口

public interface IFilterable<O> {
    public boolean include(O value);    
}

和一个ComboBoxModel实现...

public class FilterableComboBoxModel extends FilteredListModel implements ComboBoxModel {

    private FilterableComboBoxModel(ComboBoxModel model) {
        super(model);
    }

    public ComboBoxModel getComboBoxModel() {
        return (ComboBoxModel) getModel();
    }

    public void setSelectedItem(Object anItem) {
        getComboBoxModel().setSelectedItem(anItem);
    }

    public Object getSelectedItem() {
        return getComboBoxModel().getSelectedItem();
    }

}

基本上,您需要做的就是将模型包装在过滤后的模型中......

ComboBoxModel originalModel = ...;
FilterableComboBoxModel filteredModel = new FilterableComboBoxModel(origialModel);
comboBox.setModel(filteredModel(originalModel));

从那里,您只需提供IFilterable接口的实现以及您需要的逻辑并将其提供给 filtereredModel。

当出现您可能需要重新过滤模型的情况时,您可以简单地调用FilterableComboBoxModel#updateFilter

于 2012-10-14T22:40:50.110 回答