实现自定义组合框模型无法覆盖某些方法

时间:2014-12-31 14:46:45

标签: java swing jcombobox swingx comboboxmodel

尝试使用MutableComboBoxModel接口实现自定义组合框模型,我希望在运行时填充动态数据,特别是从数据库中查询。但我没有覆盖一些方法。有人可以帮助克服或提出一个更好的方法来实现我的目标吗?

package PiggeryManagementSystem;

import java.util.ArrayList;
import javax.swing.MutableComboBoxModel;
import javax.swing.event.ListDataListener;

/**
 * @author Stanchart
 */
public class MyComboBoxModel  implements MutableComboBoxModel {

    ArrayList <String> columns = new ArrayList();

    public MyComboBoxModel( ArrayList<String> c) {
        this.columns = c;
    }

    @Override
    public void addElement(Object item) {

    }

    @Override
    public void removeElement(Object obj) {

    }

    @Override
    public void insertElementAt(Object item, int index) {
        columns.get(index);
    }

    @Override
    public void removeElementAt(int index) {

    }

    @Override
    public void setSelectedItem(Object anItem) {

    }

    @Override
    public Object getSelectedItem() {
        return columns.getClass();
    }

    @Override
    public int getSize() {
        return columns.size();
    }

    @Override
    public Object getElementAt(int index) {
        return columns.get(index);
    }

    @Override
    public void addListDataListener(ListDataListener l) {

    }

    @Override
    public void removeListDataListener(ListDataListener l) {

    }

}

1 个答案:

答案 0 :(得分:0)

我已经成功解决了这个问题。非常感谢。

package PiggeryManagementSystem;

import java.util.ArrayList;
import javax.swing.MutableComboBoxModel;
import javax.swing.event.ListDataListener;

/**
 * @author Stanchart
 */
public class MyComboBoxModel  implements MutableComboBoxModel {

    private Object selectedItem;     
    ArrayList <String> columns = new ArrayList();

    public MyComboBoxModel( ArrayList<String> c) {
       this.columns = c;
    }

    @Override
    public void addElement(Object item) {
        columns.add(item.toString());
    }

    @Override
    public void removeElement(Object obj) {
        columns.remove(obj.toString());
    }

    @Override
    public void insertElementAt(Object item, int index) {
        columns.get(index);
    }

    @Override
    public void removeElementAt(int index) {
        columns.remove(index);
    }

    @Override
    public void setSelectedItem(Object anItem) {
        selectedItem = anItem;   
    }

    @Override
    public Object getSelectedItem() {
        return selectedItem;
    }

    @Override
    public int getSize() {
        return columns.size();
    }

    @Override
    public Object getElementAt(int index) {
        return columns.get(index);
    }

    @Override
    public void addListDataListener(ListDataListener l) {

    }

    @Override
    public void removeListDataListener(ListDataListener l) {

    }    
}